Question
Accenture India Private Limited
IN
Last activity: 3 Mar 2023 1:35 EST
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
2) 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.
***Edited by Moderator Marije to change type from Pega Academy to Product***
-
Like (0)
-
Share this page Facebook Twitter LinkedIn Email Copying... Copied!
Accepted Solution
Updated: 3 Mar 2023 1:35 EST
TSPi
IN
Please write this code in edit validate, and confugure this on the property rule Advanced.
String regex= "^[A-Za-z -]+$";
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");
Tata Consultancy Services
IN
Hi @BhavaniB1841 Please refer my previous post, you will get some idea.
https://support.pega.com/question/edit-validate-rule-creation
I hope this will help you.
Thanks,
Ashok
Accepted Solution
Updated: 3 Mar 2023 1:35 EST
TSPi
IN
Please write this code in edit validate, and confugure this on the property rule Advanced.
String regex= "^[A-Za-z -]+$";
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");
-
Mandeep Rawat
Updated: 24 Feb 2023 1:07 EST
Accenture India Private Limited
IN
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
TSPi
IN
@BhavaniB1841 There is spelling mistake in your second screenshot
Use the code from the image I am attaching.
Accenture India Private Limited
IN
@Satish Sudagoni - Thanks for the reply
it should not accept the _ and space as first character, it should accept only alphabet as first character
Bits in Glass
CA
String input = "Hello World-2021";
if (input.matches("^[A-Za-z -]+$"))
{ System.out.println("Input contains only alphabets, spaces, or hyphens"); }
else
{ System.out.println("Input contains other characters as well"); }
Accenture India Private Limited
IN
@MandeepRawat Thanks for the reply
but first character should be space or _ , it should be only alphabet
Updated: 3 Mar 2023 1:35 EST
Bits in Glass
CA
String input = "Hello World-";
if (input.matches("^[A-Za-z][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