You must Sign In to post a response.
  • Category: ASP.NET

    How to Validate Input value should be between 0.1 to 4 using Regular Expression Validator in asp.net

    Hi Developers,
    I am creating a project .
    in my project Text Box value should be accept between from 0.1 to 4.0 only.
    Valid=0.1,1.0,2.5,3.9,4.0
    Invalid= 4.1, 4.5
    i need to validate this.
    i have no idea now i am googling it .
    if anyone of you Know how to done it Please help me.

    Thanks with
    Paul.S
  • #766158
    you can use
    [0-3]+\.[0-9]{1,1}

    also
    you can use parse Float to compare values

  • #766160
    Hi,
    Use expression <font color='blue'> ^(?:4(?:\.0)?|[1-3](?:\.[0-9])?|0?\.[1-9])$ </font>
    <font color='brown'> 0?\.[1-9] means range 0.1 to 0.9 </font>
    <font color='brown'> 1-3](?:\.[0-9])? means range 1.0 to 3.9 </font>
    <font color='brown'> 4(?:\.0)? means exact 4.0 </font>
     
    string szInput= "0.1";
    Regex regex123 = new Regex(@"^(?:4(?:\.0)?|[1-3](?:\.[0-9])?|0?\.[1-9])$");
    var vResult = regex123.Matches(szInput).Cast<Match>().ToList();

    Your vResult will contain value only when your szInput string will pass through the expression.

  • #766207
    Thanks mr.Shashi , thanks for your reply

  • #766212
    use below regular expression
    0?\.[1-4] | 1?\.[1-4] | | 0[1-4]
    it allows number from 0.1 to 0.4 . and 1 to 4
    see if it works
    if (!System.Text.RegularExpressions.Regex.IsMatch(textbox.Text, "0?\.[1-4] | 1?\.[1-4] | | 0[1-4] "))
    {
    //Not allowed
    }
    else
    {
    //allowed
    }

    Thanks
    Koolprasd2003
    Editor, DotNetSpider MVM
    Microsoft MVP 2014 [ASP.NET/IIS]


Sign In to post your comments