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 ?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With