Javascript/jquery - Float Validating?
Solution 1:
Here are the major types of validation.
$(document).ready(function(){
//for numeric integer onlyvar num_int_Exp = /^[0-9]+$/;
$("body").on("keypress", ".custom_numeric_int", function(e){
var keynum;
if(window.event){ // IE
keynum = e.keyCode;
}elseif(e.which){ // Netscape/Firefox/Opera
keynum = e.which;
}
if(!String.fromCharCode(keynum).match(num_int_Exp))
{
returnfalse;
}
returntrue;
});
//for numeric float only
$("body").on("keypress", ".custom_numeric_float", function(e){
//$('.custom_numeric_float').keypress(function(event)//alert($(this).val().indexOf("."));if ($(this).val().indexOf(".") > -1 && event.which == 46) {
returnfalse;
}
if ((event.which != 46) && (event.which < 48 || event.which > 57)) {
event.preventDefault();
}
});
//for character inputvar charExp = /^[a-zA-Z]+$/;
$("body").on("keypress", ".custom_char", function(e){
var keynum;
if(window.event){ // IE
keynum = e.keyCode;
}elseif(e.which){ // Netscape/Firefox/Opera
keynum = e.which;
}
if(!String.fromCharCode(keynum).match(charExp))
{
returnfalse;
}
returntrue;
});
//for alpha-numeric inputvar alphaExp = /^[a-zA-Z0-9]+$/;
$("body").on("keypress", ".custom_alphanumeric", function(e){
var keynum;
if(window.event){ // IE
keynum = e.keyCode;
}elseif(e.which){ // Netscape/Firefox/Opera
keynum = e.which;
}
if(!String.fromCharCode(keynum).match(alphaExp))
{
returnfalse;
}
returntrue;
});});
Now give the appropriate class to your textbox and you are done with validation. Hope this will help.
Solution 2:
You could solve the input-price issue with a Masked Input Plugin. There are more on the jQuery website.
Validating data can be done on format and content. When done on format you can use a jQuery validate plugin. For format I would use a webservice for the adresses, but for State and Country you could provide a dropdown (input-select) with a mandatory selection.
Zipcode e.g differs per country, so you have to make that dependent to the country selection ;)
Solution 3:
Code for decimal position:
<input type="text"class="price" value="" />
$('.price').keydown(function() {
var decPos = $(this).val().split('.');
if(decPos.length > 1)
{
decPos = decPos[1];
if(decPos.length >= 2) returnfalse;
}
});
Validating address info would probably found here - http://docs.jquery.com/Plugins/validation
Solution 4:
I think the best choice is using Regular Expressions. More info.
For Decimal examples:
var decimal = /\.\d\d$/;
Post a Comment for "Javascript/jquery - Float Validating?"