locked
validating number with commas RRS feed

  • Question

  • User-2060576634 posted

    Hi everyone.. I currently have a textbox input that receives numbers from users but with commas.. for example: 10,000,000
    it seems obvious that I couldn't validate this input to make sure it's a numeral using the validation helper . so I decided to first trim the commas out of the input using:

    var myinput="";
    myinput= new String(Request.Form["myinput"].Where(Char.IsLetterOrDigit).ToArray());

    and then do some manual validation like:

            int res;
            if (int.TryParse(myinput, out res))
            {
                
            }
            else
            {
                Response.Redirect("~/");
            }
    

    now I was wondering whether the procedure above is good practice for validating and how can I validate larger numerals rather than being limited to int?

    Saturday, September 26, 2015 9:17 AM

Answers

  • User325035487 posted

    You have many options.

    1. Use Jquery Validate - http://jqueryvalidation.org/number-method/
    2. Use Web Pages as follows
    int myinput;
    Validation.Add("number", 
            Validator.Required("Make sure you say how many you want"),
            Validator.Range(1,4, "Must be between 1 and 4")
            );
    if(IsPost)
    {
      if (Validation.IsValid()) 
         {
          myinput = Request["number"]
         }
      else{
          ModelState.AddFormError("There are some errors with your submission"); 
          }
    }
    
    
    //Input Field Like this
    <div>@Html.TextBox("number", Request["number"])</div>
    

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, September 26, 2015 1:04 PM

All replies

  • User753101303 posted

    Hi,

    The point is that parsing a number (or a date) depends on the particular country convention which is in currently in use. See https://msdn.microsoft.com/en-us/library/bz9tc508.aspx for how to define it (once for all in the web.config or dynamically depending on user preference). If using the country convention you (or the user) really want you shouldn't have any more to alter the user input before trying to validate it.

    Saturday, September 26, 2015 9:47 AM
  • User325035487 posted

    You have many options.

    1. Use Jquery Validate - http://jqueryvalidation.org/number-method/
    2. Use Web Pages as follows
    int myinput;
    Validation.Add("number", 
            Validator.Required("Make sure you say how many you want"),
            Validator.Range(1,4, "Must be between 1 and 4")
            );
    if(IsPost)
    {
      if (Validation.IsValid()) 
         {
          myinput = Request["number"]
         }
      else{
          ModelState.AddFormError("There are some errors with your submission"); 
          }
    }
    
    
    //Input Field Like this
    <div>@Html.TextBox("number", Request["number"])</div>
    

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, September 26, 2015 1:04 PM