Javascript/jQuery - Float Validating?

I have a textfield that a person can input a price into (XX.xx). Is there a way to make sure they only put in no more than 2 digits after the decimal?

Answers:

Answer

Here are the major types of validation.

$(document).ready(function(){
//for numeric integer only
var num_int_Exp = /^[0-9]+$/;
$("body").on("keypress", ".custom_numeric_int", function(e){
        var keynum;
            if(window.event){ // IE
                keynum = e.keyCode;
            }else
            if(e.which){ // Netscape/Firefox/Opera                  
                    keynum = e.which;
            }
            if(!String.fromCharCode(keynum).match(num_int_Exp))
            {
                return false;
            }
            return true;
});

//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) {
            return false;
        }

        if ((event.which != 46) && (event.which < 48 || event.which > 57)) {
            event.preventDefault();
        }
});


//for character input
var charExp = /^[a-zA-Z]+$/;
$("body").on("keypress", ".custom_char", function(e){
            var keynum;
            if(window.event){ // IE
                keynum = e.keyCode;
            }else
            if(e.which){ // Netscape/Firefox/Opera                  
                    keynum = e.which;
            }
            if(!String.fromCharCode(keynum).match(charExp))
            {
                return false;
            }
            return true;
});

//for alpha-numeric input
var alphaExp = /^[a-zA-Z0-9]+$/;
$("body").on("keypress", ".custom_alphanumeric", function(e){
            var keynum;
            if(window.event){ // IE
                keynum = e.keyCode;
            }else
            if(e.which){ // Netscape/Firefox/Opera                  
                    keynum = e.which;
            }
            if(!String.fromCharCode(keynum).match(alphaExp))
            {
                return false;
            }
            return true;
});});

Now give the appropriate class to your textbox and you are done with validation. Hope this will help.

Answer

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 ;)

Answer

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) return false;
    }
});

Validating address info would probably found here - http://docs.jquery.com/Plugins/validation

Answer

I think the best choice is using Regular Expressions. More info.

For Decimal examples:

var decimal = /\.\d\d$/;

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.