By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,382 Members | 1,643 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,382 IT Pros & Developers. It's quick & easy.

question about whitespace

P: n/a
I've created a function that checks form fields that only will have letters.
This is the script:

<script type="text/javascript" language="javascript">
function validateString(field, msg, min, max) {
if (!min) { min = 1 }
if (!max) { max = 65535}
if (!field.value || field.value.length < min || field.value.max > max) {
alert(msg);
field.focus();
field.select();
return false;
}
return true;
}
</script>

How can I add code to this function that checks for whitespace...ex.
"\n\r\t"
I would also like to check for only letters in the fields and make sure no
numbers have been entered. Any help is appreciated. Thanks in advance.
-D-
Jul 20 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Using regular expressions would be the easiest method:

/^[a-zA-z]+$/ only allow letters

<script type="text/validate">
function Validate()
{
// set pattern for text only
var textRE=/^[a-zA-z]+$/;

varTextObject1=document.forms['myform'].elements['mytextbox'];

// check contents of textbox 'mytextbox' in form 'myform' against pattern
if(!textRE.test(TextObject1.value)
{
alert('text field 1 can only accept text, not numbers or other
characters.');
TextObject1.focus();
return false;
}
return true;
}
</script>

.......

<form name="myform" action="blap.php" method="get" onSubmit="return
Validate();">
<input type="text" name="mytextbox">
<input type="submit" value="submit form">
</form>
for numbers only use /^[0-9]+$/
for mixed text and numbers, with spaces /^[0-9a-zA-Z\s]+$/

"Dwayne Epps" <dw**************@centurytel.net> wrote in message
news:5T********************@feed2.centurytel.net.. .
I've created a function that checks form fields that only will have letters. This is the script:

<script type="text/javascript" language="javascript">
function validateString(field, msg, min, max) {
if (!min) { min = 1 }
if (!max) { max = 65535}
if (!field.value || field.value.length < min || field.value.max > max) {
alert(msg);
field.focus();
field.select();
return false;
}
return true;
}
</script>

How can I add code to this function that checks for whitespace...ex.
"\n\r\t"
I would also like to check for only letters in the fields and make sure no
numbers have been entered. Any help is appreciated. Thanks in advance.
-D-

Jul 20 '05 #2

P: n/a
"Dwayne Epps" <dw**************@centurytel.net> wrote in message news:<5T********************@feed2.centurytel.net> ...
I've created a function that checks form fields that only will have letters.
This is the script:

<script type="text/javascript" language="javascript">
function validateString(field, msg, min, max) {
if (!min) { min = 1 }
if (!max) { max = 65535}
if (!field.value || field.value.length < min || field.value.max > max) {
alert(msg);
field.focus();
field.select();
return false;
}
return true;
}
</script>

How can I add code to this function that checks for whitespace...ex.
"\n\r\t"
I would also like to check for only letters in the fields and make sure no
numbers have been entered. Any help is appreciated. Thanks in advance.
-D-

Hi!
Well, did you know the regular expressions?!
Take a look!

funcion noNumbers( field ) {
if( field.value.match( /\d/ ) {
alert('The string MUST not contain numbers!');
}
}

function removeSpaces( field ) {
field.value.replace( /\s/g, '' );
}

Here are some useful regular expressions:
[a-zA-Z] any letter
\d any number; same as [0-9]
\D any NOT number; same as [^0-9]
\w any alphanumeric character; same as [a-zA-Z-0-9_]
\W any NON-alphanumeric character; same as [^a-zA-Z0-9_]
\s any whitespace (tab, space, newline, etc...)
\S any NON-whitespace
\n newline
\t tab

Try search informations about regular expressions in JavaScript!

HTH
--
Edoardo
[http://edoardoontheweb.interfree.it/index.html]
Jul 20 '05 #3

P: n/a
Hi Eduardo,
Thanks for the replying to my post. I appreciate the help. I was
looking over the regular expressions as you suggested and got the check for
whitespace and numbers to work, but I am having difficulty validating
against irregular characters, i.e., !#$%^&*()+=?).

I'm using the function listed below to validate the form fields. So, if I
wanted to validate against irregular characters, what regular expression
could I use to check for the following irregular characters: !#$%^&*()+=?).
Thanks again for your help! I'm pretty new with Javascript, so I really
appreciate the help on this.

function noNumbers(field) {
if (field.value.match(/\d/)) {
alert('This field can not contain numbers!');
field.focus();
field.select();
return false;
}
return true;
}

-D-
"edoardo" <ed*************@interfree.it> wrote in message
news:85**************************@posting.google.c om...
"Dwayne Epps" <dw**************@centurytel.net> wrote in message

news:<5T********************@feed2.centurytel.net> ...
I've created a function that checks form fields that only will have letters. This is the script:

<script type="text/javascript" language="javascript">
function validateString(field, msg, min, max) {
if (!min) { min = 1 }
if (!max) { max = 65535}
if (!field.value || field.value.length < min || field.value.max > max) {
alert(msg);
field.focus();
field.select();
return false;
}
return true;
}
</script>

How can I add code to this function that checks for whitespace...ex.
"\n\r\t"
I would also like to check for only letters in the fields and make sure no numbers have been entered. Any help is appreciated. Thanks in advance.
-D-

Hi!
Well, did you know the regular expressions?!
Take a look!

funcion noNumbers( field ) {
if( field.value.match( /\d/ ) {
alert('The string MUST not contain numbers!');
}
}

function removeSpaces( field ) {
field.value.replace( /\s/g, '' );
}

Here are some useful regular expressions:
[a-zA-Z] any letter
\d any number; same as [0-9]
\D any NOT number; same as [^0-9]
\w any alphanumeric character; same as [a-zA-Z-0-9_]
\W any NON-alphanumeric character; same as [^a-zA-Z0-9_]
\s any whitespace (tab, space, newline, etc...)
\S any NON-whitespace
\n newline
\t tab

Try search informations about regular expressions in JavaScript!

HTH
--
Edoardo
[http://edoardoontheweb.interfree.it/index.html]

Jul 20 '05 #4

P: n/a
"Dwayne Epps" <dw**************@centurytel.net> wrote in message news:<KD********************@feed2.centurytel.net> ...
Hi Eduardo,
Thanks for the replying to my post. I appreciate the help. I was
looking over the regular expressions as you suggested and got the check for
whitespace and numbers to work, but I am having difficulty validating
against irregular characters, i.e., !#$%^&*()+=?).

I'm using the function listed below to validate the form fields. So, if I
wanted to validate against irregular characters, what regular expression
could I use to check for the following irregular characters: !#$%^&*()+=?).
Thanks again for your help! I'm pretty new with Javascript, so I really
appreciate the help on this.

function noNumbers(field) {
if (field.value.match(/\d/)) {
alert('This field can not contain numbers!');
field.focus();
field.select();
return false;
}
return true;
}

-D-


Well, you can modify the function like this:

function noNumbers(field) {
if( field.value.match( /[!#$%^&*()+=?]/)\ ) ) {
alert('This field can not contain numbers!');
field.focus();
field.select();
return false;
}
return true;
}

The square brackets [] means that the matching returns 1 if at least a
character present between them is found.
You can add also \d if you want to match also numbers.

Bye
--
Edoardo
[http://edoardoontheweb.interfree.it/index.html]
Jul 20 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.