By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,915 Members | 1,350 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,915 IT Pros & Developers. It's quick & easy.

regular expression to check a string is alphanumeric only

P: n/a
I want to check if the user enters alphabet or numbers only in the
text box. If the user enters non-alphabet or non-numbers, I should pop
up a message and doesn't allow the user to do that. I am using regular
expression to do the checking. But it seems it always return false.
What did I miss? please advise. thanks!!

<script type="text/javascript">
function checkkey(obj)
{ var re = /^[a-zA-Z_0-9]$/;
alert(re.test(obj.value));
if (! re.test(obj.value))
{ alert("Please enter alphanumeric only");
}
}
</script>

<form name="InputForm">
<P><input type="text" name="username"
onKeyPress="checkkey(InputForm.username)">
</form>
Jul 23 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Matt wrote on 21 sep 2004 in comp.lang.javascript:
I want to check if the user enters alphabet or numbers only in the
text box. If the user enters non-alphabet or non-numbers, I should pop
up a message and doesn't allow the user to do that. I am using regular
expression to do the checking. But it seems it always return false.
What did I miss? please advise. thanks!!

<script type="text/javascript">
function checkkey(obj)
{ var re = /^[a-zA-Z_0-9]$/;
alert(re.test(obj.value));
if (! re.test(obj.value))
{ alert("Please enter alphanumeric only");
}
}
</script>

<form name="InputForm">
<P><input type="text" name="username"
onKeyPress="checkkey(InputForm.username)">
</form>


try:

<script type="text/javascript">
function checkkey(v) {
if (/\W/.test(v.value)) {
alert("Please enter alphanumerics only");
return false;
}
return true;
}
</script>

<form>
<input type="text" onKeyUp="return checkkey(this)">
</form>

=============

\W is a shortcut for [^a-zA-Z0-9_]

onKeyPress() will be one letter late, so use onKeyUp()

"this" will uniquely reference your object

the "return true/false" will prohibit the disallowed char displaying

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress,
but let us keep the discussions in the newsgroup)

Jul 23 '05 #2

P: n/a
Evertjan. wrote:

the "return true/false" will prohibit the disallowed char displaying


Doesn't seem to for me in any browser I tried.

If the requirement is to not display disallowed characters
at all, the following works:

<script type="text/javascript">
var a = '';
function checkKey(v) {
if (/\W/.test(v.value)) {
alert('Please enter alpha numeric characters only');
v.value = a;
}else{
a = v.value;
}
}
</script>
Jul 23 '05 #3

P: n/a
RobG wrote on 22 sep 2004 in comp.lang.javascript:
Evertjan. wrote:

the "return true/false" will prohibit the disallowed char displaying


Doesn't seem to for me in any browser I tried.


Did you try IE? That's the one tested OK by me.

Please do not implement my code but test it first.

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress,
but let us keep the discussions in the newsgroup)

Jul 23 '05 #4

P: n/a
Evertjan. wrote:
RobG wrote on 22 sep 2004 in comp.lang.javascript:
Evertjan. wrote:
the "return true/false" will prohibit the disallowed char displaying


Doesn't seem to for me in any browser I tried.


Did you try IE? That's the one tested OK by me.


Yes, IE 6.0.2800.1106.xpsp2. Also Firefox 0.9.3 and Mozilla
1.7.2. The code I posted works in all these browsers.
Jul 23 '05 #5

P: n/a
Evertjan. wrote:
Did you try IE? That's the one tested OK by me.


Yes, IE 6.0.2800.1106.xpsp2. Also Firefox 0.9.3 and Mozilla
1.7.2
Jul 23 '05 #6

P: n/a
RobG wrote on 23 sep 2004 in comp.lang.javascript:
Evertjan. wrote:
RobG wrote on 22 sep 2004 in comp.lang.javascript:
Evertjan. wrote:

the "return true/false" will prohibit the disallowed char displaying

Doesn't seem to for me in any browser I tried.


Did you try IE? That's the one tested OK by me.


Yes, IE 6.0.2800.1106.xpsp2. Also Firefox 0.9.3 and Mozilla
1.7.2. The code I posted works in all these browsers.


I thought you were referring to the code I(!) posted not working?

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress,
but let us keep the discussions in the newsgroup)

Jul 23 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.