Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular Expression - Only first character check non-letter characters

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 ?

like image 518
A. Mesut Konuklar Avatar asked Nov 06 '13 19:11

A. Mesut Konuklar


1 Answers

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.

like image 83
Trenton Trama Avatar answered Nov 13 '22 13:11

Trenton Trama