1

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

Peru
  • 2,871
  • 5
  • 37
  • 66
  • just using => required: true,number: true , How can you validate decimal number? – Pratik Joshi Mar 27 '14 at 07:35
  • I think you need to add custom function to solve the problem, means validate the value with custom function where it validate required type of value. – Niju Mar 27 '14 at 07:37

2 Answers2

1

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.

Sparky
  • 98,165
  • 25
  • 199
  • 285
  • Thanks,Am using C# and i did some analyze on that part.just curious to know Is at all a way to handle tis with jquery custom functions? Yes i did tried with Regex part but not worked out ! – Peru Mar 28 '14 at 05:41
  • @Peru, I would never do something on the client-side that has little to do with GUI and can be easily achieved on the server-side. Think about it, client-side function are for a better user experience but can be broken or bypassed and depend on browser version. Server-side functions are more reliable and independent of the user's equipment. – Sparky Mar 29 '14 at 14:21
0

Use custom function in addMethod()

var stri = value.val();
if(typeof stri === 'number' && isFinite(stri) ) //returns true if number
    return true;
else 
    return false;
Pratik Joshi
  • 11,485
  • 7
  • 41
  • 73
  • Still 1.23456789123457E+15 is considered a string with this custom function – Peru Mar 27 '14 at 08:56
  • I would follow @Sparky's advice in this case. However, for anyone arriving here looking for a way to _allow_ users entering numbers in scientific notation, have a look at [Validate decimal numbers in JavaScript - IsNumeric()](http://stackoverflow.com/a/1830844/456456) – R. Schreurs Dec 21 '15 at 08:44