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>"" + aStrings[j] + ""<\/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