Regular Expression Validation in MVC4

Prashant Mehta

I want to allow double data type as

Enter valid price , Maximum upto 6 digits or 4 digits real and 2 fractional

    [Required]
    [RegularExpression(@"^\d+(\.\d{4,2})?$", ErrorMessage = "Enter valid Price, Maximum upto 6 digits or 4 digits real and 2 fractional.")]
    public double Price { get; set; }

I am getting a error on view page as parsing "^\d+(\.\d{4,2})?$" - Illegal {x,y} with x > y.

Help is really appreciated. Thanks

npinti

In your regular expression syntax, {4,2} means at least 4 times and at most 2 times. This is what is causing the error since 4 is greater than 2. Invert the order and it should work.

That being said, I am not sure if the expression will do what you are after. You can try to use ^(\d{1,6})|(\d{1,4}\.\d{1,2})$ to match either an integer with at most 6 digits or else a fraction with up to 4 digits in the integer part and up to 2 digits in the fraction part.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related