Javascript Function for Validation on Input fields ( Useful for Number,Letters,Characters,Currency validations)
Hi Guys,
Many a times we have requirement where we need to have validation on Input field to let user enter just number, just letters or related to currency etc.
Below you can find a single javascript function to handle all situation related to numbers, characters, currency, specialCharacters.
// This function is being used for providing different validation on your text box as per your need
function inputLimiter(e,allow) {
var AllowableCharacters = '';
if (allow == 'Letters'){AllowableCharacters=' ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';}
if (allow == 'Numbers'){AllowableCharacters='1234567890';}
if (allow == 'NameCharacters'){AllowableCharacters=' ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-.\'';}
if (allow == 'NameCharactersAndNumbers'){AllowableCharacters='1234567890 ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-\'';}
if (allow == 'Currency'){AllowableCharacters='1234567890.';}
var k = document.all?parseInt(e.keyCode): parseInt(e.which);
if (k!=13 && k!=8 && k!=0){
if ((e.ctrlKey==false) && (e.altKey==false)) {
return (AllowableCharacters.indexOf(String.fromCharCode(k))!=-1);
} else {
return true;
}
} else {
return true;
}
}
</script>
Just call the above function onKeyPress of input field and pass Argument as per your requirement.
For ex:
1) Limit users to enter only number:
onkeypress="return inputLimiter(event,'Numbers');"
2) Restrict user to enter only characters (both small and Caps)
onkeypress="return inputLimiter(event,'Letters');"
3) Restrict user to enter just currency (with decimals)
onkeypress="return inputLimiter(event,'Currency');"
4) Restrict user to enter characters and numbers
onkeypress="return inputLimiter(event,'NameCharactersAndNumbers');"
5) Restrict user to enter Name Characters only (i.e. with - and . between names)
onkeypress="return inputLimiter(event,'NameCharacters');"
Many a times we have requirement where we need to have validation on Input field to let user enter just number, just letters or related to currency etc.
Below you can find a single javascript function to handle all situation related to numbers, characters, currency, specialCharacters.
Javascript code:
<script type="text/javascript">// This function is being used for providing different validation on your text box as per your need
function inputLimiter(e,allow) {
var AllowableCharacters = '';
if (allow == 'Letters'){AllowableCharacters=' ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';}
if (allow == 'Numbers'){AllowableCharacters='1234567890';}
if (allow == 'NameCharacters'){AllowableCharacters=' ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-.\'';}
if (allow == 'NameCharactersAndNumbers'){AllowableCharacters='1234567890 ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-\'';}
if (allow == 'Currency'){AllowableCharacters='1234567890.';}
var k = document.all?parseInt(e.keyCode): parseInt(e.which);
if (k!=13 && k!=8 && k!=0){
if ((e.ctrlKey==false) && (e.altKey==false)) {
return (AllowableCharacters.indexOf(String.fromCharCode(k))!=-1);
} else {
return true;
}
} else {
return true;
}
}
</script>
Just call the above function onKeyPress of input field and pass Argument as per your requirement.
For ex:
1) Limit users to enter only number:
onkeypress="return inputLimiter(event,'Numbers');"
2) Restrict user to enter only characters (both small and Caps)
onkeypress="return inputLimiter(event,'Letters');"
3) Restrict user to enter just currency (with decimals)
onkeypress="return inputLimiter(event,'Currency');"
4) Restrict user to enter characters and numbers
onkeypress="return inputLimiter(event,'NameCharactersAndNumbers');"
5) Restrict user to enter Name Characters only (i.e. with - and . between names)
onkeypress="return inputLimiter(event,'NameCharacters');"
Comments
Post a Comment