javascript - Regex expression is not working? -


I am trying to create a regex expression with this requirement.

Requirement:

Maximum Length - 5 (inc decimal point if decimal number is)

Decimal Accession - Maximum 2 digits (if it is a decimal number).

Number - no need to be a decimal number (not mandatory)

Code:

 < Code> & lt; Script & gt; Function myFunction () {var regexp = / ^ (????? \\.? $) \ D {0,5} (\. \ D {0,2})? $ /; Var num = 12345.52; // I will test indifernt methods var n = regexp.test (num) document.getElementById ("Demo") here. InnerHTML = n; // returns true or false} & lt; / Script & gt;   

The output should look like:

12345.52 - & gt; It should be returned to false because length is 8 inc dot but this return true

123456.52 - & gt; false . I found out d {0,5} looking for decimal before

12.45 - & gt; truth . Correct One (Length 5, Precision 2)

12345 - & gt; truth .

I am hoping to make a Rizax expression, I satisfy all the above situations.

Reference:

You can use the regex given below.

  ^ (? =. {1,5} $) \ D + (?: \. \ D {1,2})? $   

Explanation:

  • ^ assures That we are in the beginning
  • (? =. {1,5} $) indicates that the length should be from 1 to 5.
  • Allows \ d + one or more digits.
  • (?: \. \ D {1,2})? After the decimal point, the optional decimal part should be one or two with acceptable digits.
  • $ indicates that we are at the end of the line

Comments

Popular posts from this blog

php - PDO bindParam() fatal error -

logging - How can I log both the Request.InputStream and Response.OutputStream traffic in my ASP.NET MVC3 Application for specific Actions? -

java - Why my included JSP file won't get processed correctly? -