TheGallery Posted February 27, 2012 Share Posted February 27, 2012 Hello guys, I am fairly new to regular expressions, and i am pretty much stuck. I am using a function to validate a form and since the first input field asks for the full name of a user, i only want the characters A-Za-z - ' and spaces to be allowed. Here is the pattern i used fnamePattern = /[A-z '-]/; and the condition i used //fullname is taking the input value earlier in the script if(!fnamePattern.test(fullname)){ error = "Bla bla bla";} With the above code the input will be valid as long as i use at least one of the characters in the pattern(even if i use any other character like @ for example), although i don't really get this. Shouldn't the condition evaluate to true and assign a value to error? Since i was stuck with this i changed my pattern to fnamePattern = /[^A-z '-]/; and my condition to //fullname is taking the input value earlier in the scriptif(fnamePattern.test(fullname)){ error = "Bla bla bla";} Now everything works fine but the ^ character is allowed too. Clearly there is something i am not getting right with RegExs. Any help will be appreciated. Link to comment Share on other sites More sharing options...
Ingolme Posted February 27, 2012 Share Posted February 27, 2012 I wouldn't use A-z, because after 'Z' and before 'a' are a whole bunch of other characters. You have to write it as A-Za-z Link to comment Share on other sites More sharing options...
TheGallery Posted February 27, 2012 Author Share Posted February 27, 2012 That seems to have fixed the second issue. I thought A-z was equal to A-Za-z. But could someone explain why the first method won't work? What am i doing wrong there. Link to comment Share on other sites More sharing options...
Ingolme Posted February 27, 2012 Share Posted February 27, 2012 In the first method, the pattern will only be false is not one single character matches the ones you're looking for. The pattern is never false as long as there's at least one of the valid characters. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.