Regex - Javascript DHTML - Developer Resourses - Validating Common Formats using Regex (Regular Expression) on Javascript ::: Lionfish Tehcnologies



Home / Developers Resourses / Tips

Validating Common Formats using Regex (Regular Expressions) on Javascript

// Validate Phone Format
var filter = /^\(?(\d{3})\)?[- ]?(\d{3})[- ]?(\d{4})$/;

// Validate Email Format
var filter = /^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,6}|[0-9]{1,3})(\]?)$/;

// Validate Date Format (Short Date mm/dd/yyyy)
var filter = /^(0?[1-9]|1[012])\/(0?[1-9]|[12][0-9]|3[01])\/(?:19|20|21\d{2})/; (Corrected 03/27/2013)
var filter = /^(?:(0[1-9]|1[012])[\/.](0[1-9]|[12][0-9]|3[01])[\/.](19|20)[0-9]{2})$/;

// Validate URL Format
var filter = /^(ftp|http|https):\/\/[A-Za-z0-9\.-]{1,}\.[A-Za-z]{2}/

// To Verify if a text has the right format
var result = filter.test(textToValidate);

// NOTE: result will be true if format is correct otherwise false.



Lionfish Technologies ® 2009 All rights reserved