Jaison
Jaison

Reputation:

Regular expression for numeric range 0000 to 9999

I need a regular expression for a text field in my asp.net website

which should lie in between

0000 to 9999

it is not be

0 to 9999

Upvotes: 2

Views: 5813

Answers (4)

Jhonny D. Cano -Leftware-
Jhonny D. Cano -Leftware-

Reputation: 18013

I think this could be:

^\d{4}$

Don't forget to escape it if you are using c#

string numReg = @"^\d{4}$";

Upvotes: 24

nilamo
nilamo

Reputation: 1942

Using a little logic. (Humorous, similar to "gigantic switch")

Ruby

def validate num
  return false unless num.length == 4
  return false unless num.to_i.between?(-1, 10000)
  num.each_char {|ch| return false unless '0123456789'.include? ch }
  true
end

puts validate '404' #false
puts validate '9321' # true
puts validate '-302' #false
puts validate 'AAAA' # false

Upvotes: 0

Scott Ivey
Scott Ivey

Reputation: 41588

Along with the other answers, you could also try this.

^[0-9]{4}$

Upvotes: 9

foobarfuzzbizz
foobarfuzzbizz

Reputation: 58705

Use a gigantic switch statement!

switch(val){
    case "0000":
      print "0000";
      break;
    // ...
    case "9999":
      print "I'm sick of typing";
      break;
 }

Upvotes: 7

Related Questions