javascript validations

December 1, 2010 § Leave a comment

Here are some commonly required validations.

Most of the time we validate form fields for null values. Ignoring the fact, entering only spaces are also null ;).

So hope the following snippet will help you in preventing users from entering only spaces in a form field.

function checkValidData(v)
{
var sRegEx=/^[0-9A-Za-z]+$/; //^[a-zA-z]+$/
if( sRegEx.test(v) ){
alert(”Good”)
return true;
} else {
alert(”Please fix”)
return false;
}
}

Some other regular expression which can be used for some important validations.

  • restrict the filed so that it accepts only numbers /^[0-9]+$/
  • field accepts mixture of text, numbers and spaces /^[0-9a-zA-Zs]+$/

Some funny replacement for traditional regular expressions

  • Any number [0-9] -> d { [0-9] can be replaced with d }
  • Not number [^0-9] -> D
  • alphanumeric character [a-zA-Z-0-9_] -> w
  • NON-alphanumeric character [^a-zA-Z-0-9_] -> W

Something useful 🙂

  • whitespace -> s
  • NON-whitespace -> S
  • newline -> n
  • tab -> t
Advertisements

Tagged: , , , , , , ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

What’s this?

You are currently reading javascript validations at ARP's Web Blog.

meta

%d bloggers like this: