How to write Validation Logic for user input required with alphabetics, space and hyphen

Hi ,

I have requirement to implement a validation logic for Text field.

The field should accept the mentioned format, alphabets with space or hypen, or alphabets with hypen, or alphabets with space

otherwise error should be thrown.

examples: ABC_XYZ PQR

  1. PQRTY RTYE_ABC

3)TYUIO

4)ABC XYZ

5)XYZTY_POI

Could you help with java code required to implement this in edit validate rule.

Hi @BhavaniB1841

Please refer my previous post, you will get some idea.

I hope this will help you.

Thanks,

Ashok

@BhavaniB1841

Please write this code in edit validate, and confugure this on the property rule Advanced.

String regex= “[1]+$”;

java.uitl.regex.Pattern p=java.util.regex.Pattern.compile(regex);

if(theValue== null || theValue.trim().equals(“”) return false;

java.util.regex.Matcher=p.matcher(theValue);

if(m.matches())

return m.matches();

else

theProperty.addMessage(" write your error message");


  1. A-Za-z - ↩︎

Hi @Satish Sudagoni ,

if(theValue== null || theValue.trim().equals(“”) return false;

in the above line “)” is missing for if condition, i have included “)” ( see the attachments) it but still i am getting few errors can you please correct this and let me know that would be helpful

Error :

Syntax error, insert “) Statement” to complete IfStatement

@BhavaniB1841

String input = “Hello World-2021”;

if (input.matches(“[1]+$”))

{ System.out.println(“Input contains only alphabets, spaces, or hyphens”); }

else

{ System.out.println(“Input contains other characters as well”); }


  1. A-Za-z - ↩︎

@Satish Sudagoni - Thanks for the reply

it should not accept the _ and space as first character, it should accept only alphabet as first character

@MandeepRawat Thanks for the reply

but first character should be space or _ , it should be only alphabet

@BhavaniB1841 There is spelling mistake in your second screenshot

Use the code from the image I am attaching.

@BhavaniB1841

String input = “Hello World-”;

if (input.matches(“[1][A-Za-z -]*$”)) {

System.out.println(“Input starts with an alphabet and contains only alphabets, spaces, or hyphens”);

} else {

System.out.println(“Input does not meet the criteria”);

}

hope this helps


  1. A-Za-z ↩︎