Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
100
rated 0 times [  102] [ 2]  / answers: 1 / hits: 50084  / 11 Years ago, wed, november 6, 2013, 12:00:00

I've started to study on javascript and regexp specially. I am trying to improve myself on generating regular expressions.



What I am trying to do is - I have a name field in my html file (its in a form ofc)



...
<label for=txtName>Your Name*: </label>
<input id=txtName type=text name=txtName size=30 maxlength=40>
...


And in my js file I am trying to check name cannot start with any NON-letter character, only for the first character.
And whole field cannoth contain any special character other than dash and space.



They cannot start with any non-letter character ;



/^[A-Z a-z][A-Z a-z 0-9]*$/


They cannot contain any symbol other than dash and space ;



/^*[[&-._].*]{1,}$/


When I am testing if it works, it doesn't work at all. In which part am I failing ?


More From » html

 Answers
14

Try /^[A-Za-z][A-Za-z0-9 -]*$/

When you include a space in the character listing ([]), it's going to be allowed in the string that's being searched.



^[A-Za-z] matches the first character and says that it must be a letter of some sort.

[A-Za-z0-9 -]*$ will match any remaining characters(if they exist) after the first character and make sure it's alpha numeric, or contains spaces or dashes.


[#74460] Tuesday, November 5, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
ryleymarkelb

Total Points: 554
Total Questions: 106
Total Answers: 95

Location: Norway
Member since Mon, May 23, 2022
2 Years ago
;