Re: question about whitespace

Giganews Newsgroups
Subject: Re: question about whitespace
Posted by:  edoardoonthew…@interfree.it (edoardo)
Date: 14 Jul 2003

"Dwayne Epps" <dwayneeppsNOSP…@centurytel.net> wrote in message news:<5TnQa.2599$Jk5.18400…@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.
> "
\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

newline
\t tab

Try search informations about regular expressions in JavaScript!

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

Replies

In response to

question about whitespace posted by Dwayne Epps on Sun, 13 Jul 2003