Tags

, , ,

Hi All,

In this post today, i want to share some field level validation codes on Characters and Integers. Often we might want to restrict the users to insert unnecessary values to a form field, in order to save the data integrity of the records and avoid any further complications.

For ex :-

  • Fields with “Telephone No / Mobile No” does not need a character value to be added in it.
  • Fields with “Country name / State name” does not need a integer value to be added in it.

or many such more based on Business Requirements…

 

In order to achieve this we make use of the “onkeypress” event, where we compare the “Ascii key codes” of the key pressed with our desired key values.

The Code goes like this :-

 

// To restrict adding Characters to Integer data Fields.

RestrictCharacters = function()
{
var key = event.keyCode;
if(!(key >= 65 && key <= 90 || key >= 97 && key <= 122))
{
event.returnValue = true;
return true;
}
else
{
event.returnValue = false;
return false;
}
}

 

// To restrict adding Integers to Character data Fields.

RestrictNumbers = function()
{
var key = event.keyCode;
if(!(key >= 48 && key <= 57))
{
event.returnValue = true;
return true;
}
else
{
event.returnValue = false;
return false;
}
}

 

The following codes need to be added to the “Onload Event” of the acting form  and call the respective functions on any desired field by attaching it to the “onkeypress” event.

crmForm.all.new_telephone.attachEvent(‘onkeypress’,RestrictCharacters );

crmForm.all.new_country.attachEvent(‘onkeypress’,RestrictNumbers);

 

Happy Coding..   🙂

Ashwani.

Advertisements