skip to Main Content

I have an input which should accept only decimals. But it should be in the number range 38-41.

For example, 38.456546545, 39.4564, 40.12321, or 41.87897. It should accept only these type decimals, not like this: 5.787 or 0.4454 or -32.4546.

2

Answers


  1. Disclaimer: I completely agree that usage of regex here unjustified.

    If you don’t know how make such a validation in js/ts, you better ask it than regex for this case.

    That being said, if you missed some information in you question, and there is absolutely zero chance to make validation without regex you can use this:

    ^(?:3[89]|4[01])(?:.d*)?$
    
    Login or Signup to reply.
  2. Note – Suggest that the regex tag be added back
    If you don’t need a regex then make your question about something else

    Most languages can convert an int to a float and visa-versa.
    So converting string data to a numeric type requires the string to be parsed.
    Like entered text literals in source code.
    If a float is required a decimal point is inserted in the literal.

    If designing a numeric input field on a form, the textual form of data that is allowed is what occurs.

    The question says Regular Expression needed.
    Like hundreds of identical type questions on SO, each uniquely differing in design,
    this is another.

    The range 38.0 – 41.0

    ^(?:(?:3[89]|40).d*|41.0*)$
    

    Explained

     ^             # Begin string
     (?:
                      #  38.0  to  40.9
        (?:
           3 [89] 
         | 40
        )
        . 
        d* 
      | 
                      #  41.0
        41 . 
        0*
     )
     $             # End string
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search