Home > HTML, JavaScript, RegEx > Few Handy Javascript functions for HTML forms

Few Handy Javascript functions for HTML forms

Here are few handy javascript function, which I use on day today basis in my Web development:

// Returns string after trimming spaces from both sides
function AllTrim(str)
{
str = LTrim(str);
str = RTrim(str);
return str;
}
//Returns string after trimming spaces from left side
function LTrim(str)
{
return str.replace(/^\s+/g,”);
}
//Returns string after trimming spaces from right side
function RTrim(str)
{
return str.replace(/\s+$/g,”);
}
// validates email form field, if you pass true, it checks for empty field as well
function checkEmail(str, required)
{
regexPattern = /^[a-zA-Z_0-9-]+(\.[a-zA-Z_0-9-]+)*@([a-zA-Z_0-9-]+\.)+[a-zA-Z]{2,7}$/;
str = AllTrim(str);
if (required == ‘undefined’ || required )
{
if( str.length == 0 )
{
return false;
}
}
else
{
if( str.length == 0 )
{
return true;
}
}
return regexPattern.test(str);
}
// returns selected radio button value from a group of radio buttons in a form
function getSelectedRadioValue(formObj, obj)
{
var selected_radio_val = ”;
for (var i=0; i < document[formObj][obj].length; i++)
{
if (document[formObj][obj][i].checked)
{
selected_radio_val = document[formObj][obj][i].value;
break;
}
}
return selected_radio_val;
}

Advertisements
Categories: HTML, JavaScript, RegEx
  1. No comments yet.
  1. No trackbacks yet.

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

%d bloggers like this: