views:

3753

answers:

4

I have use an expression for validating positive number

^\d*.{0,1}\d+$

when i give input -23 then it check it is negetive but when i give input +23 then showing invalid number.

what is the problem ????

any one can give a solution that With +23 it will return (positive)

A: 
^\+?\d*.{0,1}\d+$

Gets you the ability to put a "+" in front of the number.

Jeff Meatball Yang
probably some error here, this expression do not work....
nazmul hasan
+5  A: 

Have you considered to use anything beside regular expressions?

If you are using the jQuery Validation Plugin you could create a custom validation method using the Validator/addMethod function:

$.validator.addMethod('positiveNumber',
    function (value) { 
        return Number(value) > 0;
    }, 'Enter a positive number.');


Edit: Since you want only regular expressions, try this one:

^\+?[0-9]*\.?[0-9]+$

Explanation:

  • Begin of string (^)
  • Optional + sign (\+?)
  • The number integer part ([0-9]*)
  • An optional dot (\.?)
  • Optional floating point part ([0-9]+)
  • End of string ($)
CMS
thanks , my demand is in regular expression and will be striping white space
nazmul hasan
what about white space
nazmul hasan
A: 

If you insist on using a regular expression, please make sure that you also capture numbers with exponents and that you allow numbers of the form .42, which is the same as 0.42.

^+?\d*.?\d+([eE]+?\d+)?$

should to the trick.

Martin Jansen
it is not my question , my question about + or - as prefix and should avoid white space
nazmul hasan
also some problem with it , it is not work properly
nazmul hasan
A: 

ok, try this;

\+?\d*\.?\d+\b
kara
no searching for better ans
nazmul hasan