Monday, February 23, 2009

Useful Javascript

It's been a while, but I'm doing a bit of javascript again. I have to perform some validation on the user's input and with regular expression it makes the validation much cleaner. The downside is the someone else who comes along and have no idea about regular expression is going to have problem maintaining the code. Oh well.

Validating Email
function validate_email(field) {
var email = document.getElementById(field).value;
return /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(email)
}

Validating Mobile Number
function validate_mobile(field) {
var mobile_no = document.getElementById(field).value;
return /^[89][0-9]{7}$/.test(mobile_no);
}

Clean and simple.

No comments:

Post a Comment