Question

Am using a validate plugin and am trying to identify what is the best way to solve the below mentioned problem

I have a decimal field [textbox] and user is allowed only to enter decimal which am validating as

someDecimal: {
        required: true,
        number: true
    }

Am not having any problem with first time save. when user tried to edit that field now text box will have a scientific notation came from Database as one below

1.23456789123457E+15

So validation is fired saing it is not a valid number.But it is a number with scientific notation.

How do i handle this and make sure no validation fires if it is a valid notation ?

Thanks

Was it helpful?

Solution

You do not have a problem with jQuery Validate, therefore I don't think you should be solving this issue with jQuery Validate.

You are validating the data input by a user a certain way... in your case, using the number rule. Presumably, this is only what you want... a number that passes the number rule.

Now when you populate the form from the value in the database, it's coming back as a string in scientific notation. This issue should not have anything to do with your client-side validation.

In other words, if a string (or scientific notation) is not valid input in the first place, then you certainly would not want a jQuery Validate rule that also allows these kinds of strings.

If you only allow the user to enter a certain kind of number into this form, then your server-side code must be written to only re-populate the form with the kind of number.

So within your server-side code, the code that retrieves the value from the database and populates the form with data, simply take the value from the database and convert it into an acceptable number, a number in the same kind of format the user would have entered originally.

Since you never mentioned your server-side language, I cannot give you exact code. However, this can be done in several ways with PHP, for example.

There are advantages to my solution: The user sees the pre-populated number in the same format as he would be expected to enter it. Everything on the client-side is viewed and entered as a number. Everything on the server-side/database is handled in scientific notation and hidden from the user.

OTHER TIPS

Use custom function in addMethod()

var stri = value.val();
if(typeof stri === 'number' && isFinite(stri) ) //returns true if number
    return true;
else 
    return false;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top