June 20, 2012

javascript regular expression for alphabets and space

js regular expression for alphabets and space

Note: A useful javascript function which i mostly use to manage names and related fields validation.


function checkName(field,title)
{

var val = $(“#”+field).val();

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

if(val.length<1)
{
$(‘#error_’+field).text(title+’ is required.’);
$(‘#error_’+field).addClass(‘error-msg’);
return false;

}
else if(!re.test(val))
{
$(‘#error_’+field).text(‘Please enter a valid name.’);
$(‘#error_’+field).addClass(‘error-msg’);
return false;
}
else
{
$(‘#error_’+field).text(”);
$(‘#error_’+field).removeClass(‘error-msg’);
return true;

}

Last updated: March 19, 2014