Home > front end >  String numbers only and characters only
String numbers only and characters only

Time:01-18

I have ContactDTO class, private ContactType contacttype;(this is enum, EMAIL, PHONENUMBEr) private String contactvalue; private Long studentId; i want to write method while creating new contact if person uses PHONENUMBER enum String contactvalue must be numbers only and if person uses EMAIL enum it can be characters too

CodePudding user response:

You could write a method that takes the enum and value and which checks the value based on the enum.

It could be as simple as this:

public void setContact(ContactType type, String value) {
  //check the value first
  switch(type) {
    case PHONENUMBER: {
      if( !StringUtils.isNumeric(value) ) {
        throw new IllegalArgumentException("phone number must be numeric");
      }
      break;
    }
    case EMAIL: {
      //note: emails contain more than just numbers and alphabetic chars
      if( !StringUtils.isAlphanumeric(value) ) {
        throw new IllegalArgumentException("email must be alphanumeric");
      }
      break;
    }
  }

   this.value = value;
}

However, you might need more types or more complex ones, so you could try to use regular expressions instead ("manually" crafted rules are faster for many simple cases but require a yet more complex design):

 enum ContactType {
   PHONENUMBER("\\d "),
   EMAIL(/*pattern for email*/);

   Pattern pattern;

   private ContactType(String regex) {
     pattern = Pattern.compile(regex);
   }

   public void validateInput(String input) {
     if(!pattern.matcher(input).matches()) {
        throw new IllegalArgumentException("input invalid");
     }
   }
 }

And setting the value:

 public void setContact(ContactType type, String value) {
   type.validateInput(value);

   this.value = value;
 }

CodePudding user response:

ContactType should be a class with fields email, phoneNumber etc. And than you can validate every field you want.

CodePudding user response:

Really not much details in the questions. Since you tagged the question with "spring-boot", I suppose we're talking (javax|jakarta).validation. stuff? In this case you could use class-level validation.

Some snippets:

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Constraint(validatedBy = ContactTypeValidator.class)
public @interface ContactTypeValid {
 
    String message() default "contact value invalid for specified type";
    Class<?>[] groups() default {};
    Class<? extends Payload>[] payload() default {};
 
}
public class ContactTypeValidator implements ConstraintValidator<ContactTypeValid, ContactDTO> {
 
    @Override
    public void initialize(ContactTypeValid constraint) {
    }
 
    @Override
    public boolean isValid(ContactDTO contactDTO, ConstraintValidatorContext context) {
        // put your logic here. e.g.
        
        // deal with possible nulls

        if(PHONENUMBER == contactDTO.getContacttype()) {
          return contactDTO.getContactvalue().chars().allMatch(Character::isDigit);
        }
        return true;
    }
}
@RestController
class MyController {

  @PostMapping("/api/contact")
  void postContact(@Valid ContactDTO contactDTO) {
    ...
  }
  

}

Annotate your ContactDTO:

@ContactTypeValid
public class ContactDTO {
  ...
}

You will also need validation implementation in your dependencies, like spring-boot-starter-validation.

Spring documentation for more: https://docs.spring.io/spring-framework/docs/current/reference/html/core.html#validation-beanvalidation


And good point from @Игорь-Ходыко. Use separate fields.

CodePudding user response:

If you just need a digit validator:

  enum ContactEnum {EMAIL, PHONENUMBER}
      private boolean validatePhoneNumber(ContactEnum type, String contactString) {
    
        if (type == ContactEnum.PHONENUMBER) {
          return contactString.chars().filter(Character::isDigit).count() == contactString.length();
        }
        return false;
      }
  • Related