Home > database >  Rails validation for string not ending with 0000
Rails validation for string not ending with 0000

Time:06-13

I am working with Ruby on Rails validations, and need to validate that a given string does not end with 0000. Could anyone provide me with such a regex please. I tried to build one but I'm not very good with regex.

CodePudding user response:

You could use a negative lookahead here:

^(?!.*0000$).*$

You could also try to match ^.*0000$ from your Ruby code and then retain any inputs which do not match to this pattern.

CodePudding user response:

format also accepts without to check for strings not matching the pattern, e.g.:

validates :string_attr, format: { without: /0000\z/,
                                  message: 'must not end with 0000' }
  • Related