473,387 Members | 1,899 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

regular expression to check a string is alphanumeric only

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
6 79382
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: AJ | last post by:
Hi All Just a quickie. A form element for a search will contain either a town or a post code. Is there any way we can check the input to see if the element contains a number (in which case it...
3
by: greenflame | last post by:
I am trying to find a regular expression that returns true in the following cases but no others. 2.0 2.4 2. 324.0e345 234e34 34.e-43 234.673
1
by: outoftherealm | last post by:
I am trying to write a regular expression to ensure the selected value is not equal to a specified string. something like !~/^(Select Salesperson)$/; Thanks!
18
by: Q. John Chen | last post by:
I have Vidation Controls First One: Simple exluce certain special characters: say no a or b or c in the string: * Second One: I required date be entered in "MM/DD/YYYY" format: //+4 How...
4
by: Ernesto | last post by:
I'm trying to get the right syntax for my regular expression. The string I'm trying to parse is: # myString Name: David Dude Right now, I'm using the following code:
4
by: gfrith | last post by:
Hi, A quick regex question which I've worked around for the time being, but would like an answer to if anyone can help. I want to match on all strings which end _id, but not those ending...
1
by: .Net Sports | last post by:
I'm trying to only allow password entries that have at least one numeric character, and 7 to 15 characters long. The below script is not filtering it properly, it keeps saying all the entries are...
6
by: lisong | last post by:
Hi All, I have problem to split a string like this: 'abc.defg.hij.klmnop' and I want to get all substrings with only one '.' in mid. so the output I expect is : 'abc.defg', 'defg.hij',...
3
by: akshalika | last post by:
I want regular expression which check for empty. pls help me.
3
by: mmbutler | last post by:
I am trying to delete the following from my file: <Body> </Body> I have tried searching for <Body>+</Body> and <Body>\s+</Body> but neither works. Thank you for any help.
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.