RegEx For Alphanumeric Characters But Not Only Numeric
Solution 1:
You can use this regex expression
^(?![0-9]*$)[a-zA-Z0-9]+$
This expression has a negative lookahead to ensure that the string does not contain numbers only.
Check the demo here:
Given your code after edit, if you want to check if the given string matches your regex, you can do something like this:
Solution 2:
You can use this regex to make sure at least one alphabet is there:
/^\w*?[a-zA-Z]\w*$/
RegEx Breakup:
^ # start
\w*? # Match 0 or more word characters (non-greedy)
[a-zA-Z] # Match an English alphabet
\w* # Match 0 or more word characters
$ # end
Solution 3:
You could use a lookahead
to check if an alphabetic character is present or not.
Regex: ^(?=.*[A-Za-z])\w+$
Explanation:
(?=.*[A-Za-z])
This looks ahead if there is an alphabet anywhere in the string. If yes then it matches the next part of regex.
Output
Not Matched:
123
_123
123_
Matched:
a_123
a12_3
asd
as_j
a_
Solution 4:
I'm not exactly sure what do you want to archive, but I guess that you can use a simple regex like
^([0-9])*[a-zA-Z]+[a-zA-Z0-9]*$
Match something that doesn't start with digits, and starts with letters (upper or lowercase) one or more times, and then match alphanumeric zero or more times.
Regarding the code you provided, you need to use the opposite logic. eventName.match(/^(?![0-9]*$)[a-zA-Z0-9]+$/)
will return true if matches, ie, if the username is valid. So you need to change it to something like
(...)
else if( ! eventName.match(/^(?![0-9]*$)[a-zA-Z0-9]+$/) )
{
// Invalid username
errorCount++;
document.getElementById('error1').innerHTML="Please Enter a Valid Name";
}
(...)
or
(...)
else if( eventName.match(/^(?![0-9]*$)[a-zA-Z0-9]+$/) )
{
// Username is valid, do your logic here.
}
(...)
Post a Comment for "RegEx For Alphanumeric Characters But Not Only Numeric"