tags:

views:

29

answers:

1

hi , i need to validate a field which should only accept value like +10.00 , -10.00

that is +/- 2digits.2decimals ..how do i do this using jquery.

it should accept without + or - also. taht is i will treat 10.10 as +10.00 and it should also accept 10 and i will treat as 10.00 and .10 and i will treat it as 00.10

+3  A: 

You don't need jquery for that, you can use an expression for this:

if (/(\+|\-)\d{2}\.\d{2}/.test(value)) {
  // validated
}

Untested though, but something like this should work fine.

Peter Kruithof
I'd use `/[+-]\d{2}\.\d{2}/`, otherwise that's fine.
Tim Pietzcker
@Peter Kruithof - i need to do an inline validation..so i can use this with .change function rite?
pradeep
@Peter Kruithof @Tim Pietzcker - i tried out like this. but always returns .bad$('#retinoscopy_ltl').change(function(){var vals = $('#retinoscopy_ltl').val();if (/[+-]\d{2}\.\d{2}/.vals) {alert('looks gud');}else{alert('bad');}});
pradeep
@pradeep I've tested the expression at http://www.regular-expressions.info/javascriptexample.html and it works just fine (be sure to leave the leading/trailing slash out when testing). I'd start debugging the value if I were you and test that using your regex.
Peter Kruithof