471,338 Members | 932 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

Regular expression , for only one word



hi,
I need to verify that the user enter only one word in a textbox.
So i thought to add a regularexpression validator and disallow the user
to enter a space between the letters he wrote.
how can i do that ?

thx
*** Sent via Developersdex http://www.developersdex.com ***
Jul 23 '05 #1
6 4432
Joe Abou Jaoude wrote:
I need to verify that the user enter only one word in a textbox.
So i thought to add a regularexpression [...]


<input ... onchange="if (yourRegExp.test(value)) alert('Invalid input')">
or
<input ... onchange="if (!yourRegExp.test(value)) alert('Invalid input')">

What you replace "yourRegExp" with depends on what you consider to be a
word. Take a look at this table for some possible values:

<body>
<script type="text/javascript">
var aRegExps = [
/^\S+$/,
/^\w+$/,
/\s/,
/\W/,
/^[a-z]+$/i
],
aStrings = [
"Foobar",
"Foo Bar",
"Foo_Bar",
"Foo.Bar",
"Foo-Bar",
" Foobar",
"Foobar ",
"Foo1Bar",
"\u0641\u0648\u0628\u0627\u0631"
],
sOutput = "<table border='1'><thead><tr><th><\/th>",
i,
j;
for (i=0; i<aRegExps.length; i++) {
sOutput += "<th>" + aRegExps[i].source + "<\/th>";
}
sOutput += "<\/tr><\/thead><tbody>";
for (j=0; j<aStrings.length; j++) {
sOutput += "<tr><td>&quot;" + aStrings[j] + "&quot;<\/td>";
for (i=0; i<aRegExps.length; i++) {
sOutput += "<td>" + aRegExps[i].test(aStrings[j]) + "<\/td>";
}
sOutput += "<\/tr>";
}
sOutput +=" <\/tbody><\/table>";
document.write(sOutput);
</script>
</body>

ciao, dhgm
Jul 23 '05 #2
Joe Abou Jaoude wrote:
hi,
I need to verify that the user enter only one word in a textbox.
So i thought to add a regularexpression validator and disallow the user to enter a space between the letters he wrote.
how can i do that ?

thx
*** Sent via Developersdex http://www.developersdex.com ***


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<style type="text/css">

input {
font: 16px tahoma;
}

</style>
</head>
<body>
<form>
<input
type="text"
name="t1" value=""
onkeydown="this.prev_value=this.value"
onkeyup="var
v=this.value;if(v!=this.prev_value)this.value=v.re place(/\s+/,'')" />
one word only !
</form>
</body>
</html>

Jul 23 '05 #3
Joe Abou Jaoude wrote:
hi,
I need to verify that the user enter only one word in a textbox.
So i thought to add a regularexpression validator and disallow the user to enter a space between the letters he wrote.
how can i do that ?

thx
*** Sent via Developersdex http://www.developersdex.com ***


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<style type="text/css">

input {
font: 16px tahoma;
}

</style>
</head>
<body>
<form>
<input
type="text"
name="t1" value=""
onkeydown="this.prev_value=this.value"
onkeyup="var
v=this.value;if(v!=this.prev_value)this.value=v.re place(/\s+/,'')" />
one word only !
</form>
</body>
</html>

Jul 23 '05 #4
This is pretty basic, but you get the idea. It would be better to
actually call this from onSubmit:

<html>
<head>
<script>
function disallowspace(val){
if(/\s/.test(val)){
alert("Too many words.");
}
}
</script>
<body>
<input type="text" value="" onblur="disallowspace(this.value)" />
</body>
</html>

Jul 23 '05 #5
Try this:

<html>
<head>
<script>
function disallowspace(){
if(/\s/.test(document.getElementById('xyz').value)){
alert("Too many words.");
return false;
}
return true;
}
</script>
<body>
<form onSubmit="return disallowspace();" id="someform"
action="some.cgi" method="get">
<input type="text" value="" id="xyz" />
<input type="submit" value="submit" />
</form>
</body>
</html>

Jul 23 '05 #6

Slight upgrade...

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<style type="text/css">

input {
font: 12px tahoma;
border: 1px dashed #c88;
padding: 4px;
}

</style>
<script type="text/javascript">

function xwhite(obj)
{
var v;
if (/ /.test(v = obj.value))
obj.value = v.replace(/ +/g,'');
}

</script>
</head>
<body onload="document.forms[0].elements[0].focus()">
<form>
<input type="text" name="t1" value=""
onkeyup="xwhite(this)" onblur="xwhite(this)" />
&amp;rarr; [one word only]
</form>
</body>
</html>
*** Sent via Developersdex http://www.developersdex.com ***
Jul 23 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by peterbe | last post: by
4 posts views Thread by Buddy | last post: by
3 posts views Thread by Ryan Taylor | last post: by
6 posts views Thread by Ludwig | last post: by
3 posts views Thread by Zach | last post: by
25 posts views Thread by Mike | last post: by
8 posts views Thread by Summercool | last post: by
reply views Thread by rosydwin | last post: by

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.