mortazavi
mortazavi

Reputation: 401

regular expression for allowing number and null value but not just whitespace like " "

i need a regular expression for checking numbers , allowing 'null' value and not allowing whitespasces;

in CustomerViewModel:

[Display(Name = "Phone")]
[RegularExpression(@"^[0-9]{4,15}", ErrorMessage = "{0} is not correct!")]
public string CustomerPhoneNumber { get; set; }

it allows numbers and 'null' value and " ". in editing mode of form, when my textbox has " " ,it does not show error! i want to show error for " " and stop posting form. how can i do that?

Upvotes: 0

Views: 1769

Answers (3)

Sundar Rajan
Sundar Rajan

Reputation: 133

Try this it will work

@"^[0-9][0-9 ]{4,15}$"

Upvotes: 0

user1642018
user1642018

Reputation:

for allowing numerical and "null" value.*provided null is in lower-case and null is a word.

([0-9a-z]+)

for allowing only numerical

([0-9]+)

Upvotes: 0

nraina
nraina

Reputation: 354

This should work for allowing only numbers without spaces

^(\s*|\d+)$

Upvotes: 1

Related Questions