Search code examples
ruby-on-railsregexruby-on-rails-3validationjquery-validation-engine

Regex for a text field validation


I am using JqueryValidationEngine to validate my forms on the client side.

I want to validate a text field using regex in a form. The condition is as follows

Edit:

Condition1:

It should not allow the user to enter single or multiple combinations of zeroes.

E.g. '0', '00,, '00000' etc.

Condition2:

It should not allow the user to enter the any multiple digit number starting with 0, but excluding the condition1. 

E.g. '01', '001, '001001' , '000001' etc.

I'm using rubular.com for checking the regex and I need two regex for not allowing the above two type of values. Thanks :)-


Solution

  • Condition 1 (updated according to OP's suggestion)

    /^[1-9]\d*$/ 
    

    Condition 2 (only digits with first 0 and atleast one non zero)

    /^0+[1-9][0-9]*$/
    

    Inverse of above

    /^((?!^0+[1-9][0-9]*$).)*$/