473,219 Members | 2,124 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,219 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 4511
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Kenneth McDonald | last post by:
I'm working on the 0.8 release of my 'rex' module, and would appreciate feedback, suggestions, and criticism as I work towards finalizing the API and feature sets. rex is a module intended to make...
4
by: peterbe | last post by:
I want to match a word against a string such that 'peter' is found in "peter bengtsson" or " hey peter," or but in "thepeter bengtsson" or "hey peterbe," because the word has to stand on its own....
1
by: Jeff | last post by:
For the life of me, I can't create a working regular expression for a schema pattern to do the following: Validates if an entire word does NOT match the entire word in the pattern. For...
4
by: Buddy | last post by:
Can someone please show me how to create a regular expression to do the following My text is set to MyColumn{1, 100} Test I want a regular expression that sets the text to the following...
3
by: Ryan Taylor | last post by:
Hello. I am trying to create a regular expression that will let me know if a string has the following criteria. Order does not matter in the string, but when building a regular expression it...
6
by: Ludwig | last post by:
Hi, i'm using the regular expression \b\w to find the beginning of a word, in my C# application. If the word is 'public', for example, it works. However, if the word is '<public', it does not...
3
by: Zach | last post by:
Hello, Please forgive if this is not the most appropriate newsgroup for this question. Unfortunately I didn't find a newsgroup specific to regular expressions. I have the following regular...
25
by: Mike | last post by:
I have a regular expression (^(.+)(?=\s*).*\1 ) that results in matches. I would like to get what the actual regular expression is. In other words, when I apply ^(.+)(?=\s*).*\1 to " HEART...
0
by: altavim | last post by:
Usually when you make regular expression to extract text you are starting from simple expression. When you got to know target text, you are extending your expression. Subsequently very hard to ready...
8
by: Summercool | last post by:
somebody who is a regular expression guru... how do you negate a word and grep for all words that is tire but not snow tire or
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

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.