regular expression to check a string is alphanumeric only

M

Matt

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>
 
E

Evertjan.

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
 
R

RobG

Evertjan. said:
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>
 
E

Evertjan.

RobG wrote on 22 sep 2004 in comp.lang.javascript:
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.
 
R

RobG

Evertjan. said:
RobG wrote on 22 sep 2004 in comp.lang.javascript:


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.
 
E

Evertjan.

RobG wrote on 23 sep 2004 in comp.lang.javascript:
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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,040
Latest member
papereejit

Latest Threads

Top