473,405 Members | 2,176 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,405 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 4519
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.