Friday, December 30, 2011

Field Validation in AJAX in an Efficient Manner

Here is a simple way to validate mandatory fields, if they a more. Don’t waste your time to write a separate code for each and every fields.

I have done a simple validation in javascript for checking that the field not be null.

Step 1: Create a hidden field and name it as of your wish (name of the field may be “MandatoryFields”).

Step 2: Give all the mandatory field names with comma separator as the default value of the hidden field as shown below.


Step 3: Put the following function in JSHeader.


function validateSupplier()

{

var f=document.forms[0];

var fieldsString= f.MandatoryFields.value;

var MandatoryFields = fieldsString.split(”,”);

var ValidatedFields=0;

for(var i=0; i

{

var fieldValue = eval(’f.’+Ma ndatoryFields [i]+’.value’);

if(trim(fieldValue)==”")

{

alert(”Please fill all the mandatory fields for Supplier Information”);

eval(’f.’+MandatoryFields[i]+’.focus()’);

return false;

break;

}

else

{

ValidatedFields=ValidatedFields+1;

if(ValidatedFields==MandatoryFields.length)

{

return true;

}

}

}

}

/* Function used to trim string */

function trim(str)

{

if(!str || typeof str != ’string’)

return “”;


return str.replace(/^[\s]+/,”).replace(/[\s]+$/,”).replace(/[\s]{2,}/,’ ‘);

}

/********** Trim Ends *******/

Step 4: Write the following code in “onclick()” of a submit button.

var success = validateSupplier();

if(success)

{

document.forms[0].submit();

}

Its the smarter way to validate similar validation for multiple fields.

We can also validate for the datatypes in the same way.

function validateSupplier()
{
var f=document.forms[0];
var fieldsString= f.MandatoryFields.value;
var MandatoryFields = fieldsString.split(”,”);
var ValidatedFields=0;
for(var i=0; i
{
var fieldValue = eval(’f.’+MandatoryFields[i]+’.value’);
if(trim(fieldValue)==”")
{
alert(”Please fill all the mandatory fields for Supplier Information”);
eval(’f.’+MandatoryFields[i]+’.focus()’);
return false;
break;
}
else
{
ValidatedFields=ValidatedFields+1;
if(ValidatedFields==MandatoryFields.length)
{
return true;
}
}
}
}
/* Function used to trim string */
function trim(str)
{
if(!str || typeof str != ’string’)
return “”;
return str.replace(/^[\s]+/,”).replace(/[\s]+$/,”).replace(/[\s]{2,}/,’ ‘);
}
/********** Trim Ends *******/

function validateSupplier()

{

var f=document.forms[0];

var fieldsString= f.MandatoryFields.value;

var MandatoryFields = fieldsString.split(”,”);

var ValidatedFields=0;

for(var i=0; i

{

var fieldValue = eval(’f.’+MandatoryFields[i]+’.value’);

if(trim(fieldValue)==”")

{

alert(”Please fill all the mandatory fields for Supplier Information”);

eval(’f.’+MandatoryFields[i]+’.focus()’);

return false;

break;

}

else

{

ValidatedFields=ValidatedFields+1;

if(ValidatedFields==MandatoryFields.length)

{

return true;

}

}

}

}

/* Function used to trim string */

function trim(str)

{

if(!str || typeof str != ’string’)

return “”;


return str.replace(/^[\s]+/,”).replace(/[\s]+$/,”).replace(/[\s]{2,}/,’ ‘);

}

/********** Trim Ends *******/

Step 4: Write the following code in “onclick()” of a submit button.

var success = validateSupplier();

if(success)

{

document.forms[0].submit();

}

Its the smarter way to validate similar validation for multiple fields.

We can also validate for the datatypes in the same way.

No comments:

Post a Comment