Regular expression - allow special characters and letters in single text box

prabhmeet
Giga Expert

Hi,

I have to write  a regular expression to allow only characters (no numbers), No spaces before and after string, and allow special characters ' " -  these 3 characters anywhere in the text field.

Please help me modify the below code - 

var pattern = /^[_A-z]*((-|\s)*[_A-z])*$/;

var name = g_form.getValue('updated_first_name');
if (!pattern.test(name)){


g_form.addErrorMessage("Only Characters are allowed");
g_form.setValue('updated_first_name','');

}

1 ACCEPTED SOLUTION

Matthew Glenn
Kilo Sage

Try this for your pattern:

/^[a-zA-Z'"-](\S)*$/

 

View solution in original post

6 REPLIES 6

asifnoor
Kilo Patron

Hi,

here is the correct pattern to accept only the mentioned characters.

pattern =  /^[A-Za-z'"_]*$/

Mark correct if this solves your problem.

Hi Prabhmeet,

just for refernce, did my code did not work? 

Hi asifnoor,

Your code also worked, only it had an underscore_ instead of hyphen-, but it was just a typing mistake.

I am not sure what's the difference between /^[A-Za-z'"_]*$/ and /^[A-Za-z'"_](\S)*$/ as both did not allow any space.

Ah okay, i thought it was underscore only. Anyways, problem got solved, that's important.

Kindly mark my comment as helpful if it worked.