↑ Grab this Headline Animator

Monday, December 8, 2008

Regular expression in the javascript.....

Zip code validation:
function validateZipCode(elementValue){
var zipCodePattern = /^\d{5}$|^\d{5}-\d{4}$/;
return zipCodePattern.test(elementValue);

If you don’t want to have a zip+4 format you can use /^\d{5}$/ as the regular expression to validate simple zip code.

var fnameRegxp = /^([a-zA-Z]+)$/;

This statement checks that only upper or lowercase case letters, repeated one or more times, pass the

validation test, which, unless you’re hoping to send your newsletter to C3PO, should be the case.

Remember when I mentioned that regular expressions can still return true if there are incorrect

characters present, provided that the correct pattern of characters is somewhere within the string?

Putting the circumflex and dollar sign at the beginning and end of the regular expression ensures that

this does not happen, and that the string is only valid if it contains just what you’re asking for.

var lnameRegxp = /^([a-zA-Z]+)$/;

var houseRegxp = /^([0-9A-Za-z]+)$/;

These then check that the surname entered is also any upper or lowercase character repeated one or more

times, and that the house name consists of just numbers and letters. You could have shortened this to

/^([\w]+)$/ using the shorthand escape code for "any word character," but that would allow underscores

to be used, which rarely feature in property names.

var pcodeRegxp = /^([A-Za-z]{1,2})([0-9]{2,3})([A-Za-z]

var telnoRegxp = /^([0-9]{11})$/;

I’ve used local examples for the post code (the UK version of a zip code) and telephone regular

expressions. UK postcodes are in a format consisting of one or two letters, followed by two or three

numbers (depending on the county), and followed again by two letters. It should be easy to see how you

could change this to match your own local form of postal or zip code and telephone number formats. The

phone number check simply ensures that the correct number of numbers is present. Following these comes

the most complex of regular expressions -- those that check for valid email addresses and URLs:

var emailRegxp = /^([\w]+)(.[\w]+)*@([\w]+)(.[\w]{2,3})

var urlRegxp = /^(http:\/\/www.|https:\/\/www.|ftp:\/

Due to sub-domains, there may be any number of characters and dots preceding the @ sign. The first of

these expressions says that any word character displayed one or more times can then be followed by a

dot, then any number of word characters displayed zero or more times, followed by the @ symbol, followed

by any word character displayed one or more times, followed by a dot and two or three word characters

repeated at least once but no more than twice, so email addresses ending in .com or will pass,

whereas would fail. Similarly, the URL may begin with either http://www. or https://www. or

ftp://www. or just www. once followed by any word character one or more times, followed by a dot and any

number of word characters at least once but no more than twice.

Finally, the date of birth check allows dates in the format dd/mm/yyyy or dd-mm-yyyy, both formats being

equally as popular:

var dobRegxp = /^([0-9]){2}(\/|-){1}([0-9]){2}(\/|-)

function noAlpha(obj){
reg = /[^0-9.,]/g;
obj.value = obj.value.replace(reg,"");

onkeydown event of the textbox(reg expression for containing only one decimal number)

function checkIsNum(){
var y = document.form1.txtSubject.value;
var filter = [0-9]*\.?[0-9]*;
if (!filter.test(y)) document.form1.txtSubject.length -=1;
if(filter.indexOf('.')!=-1) document.form1.txtSubject.length --;

function isValidNumAlpha()
var c= event.keyCode;
event.keyCode=(!((c>=65 && c<=90)||(c>=97 && c<=122)|| (c==32)|| (c>=48 && c<=57)


function alphaNumWithoutSpace(text)
var c= event.keyCode;
event.keyCode=(!((c>=65 && c<=90)||(c>=97 && c<=122)||(c>=48 && c<=57)


/^[\da-z]+$/i // Only alphanumeric
/[a-z]/i // At least one letter
/\d/ // At least one number

Submit this story to DotNetKicks

No comments:

Post a Comment

Post your comments/questions/feedback for this Article.


Latest Articles