locked
validating email/mobile - different validation behavior RRS feed

  • Question

  • User379720387 posted

    I have unexplained behavior differences between these two validations, although they appear the same.

    When testing the email validation by entering a , instead of a .  as in myemail@gmail,com, the validation message appears as expected as soon as I tab to the next field.

    The regex for mobile is number only and 10 digits. When I enter AZ05551212 and tab to the next field, no message appears. It does show up after I submit the form.

    Why is this behavior so different?

    <div class="form-group">
                    <label for="email" class="col-md-4 control-label @if (!ModelState.IsValidField("email")) {<text>error-label</text>}">Email</label>
                    <div class="col-md-4">
                        <input id="email" name="email" placeholder="email address" class="form-control input-md" type="text" @Validation.For("email")>
                        @Html.ValidationMessage("email")
                    </div>
                </div>
    
                
    <div class="form-group">
                    <label for="mobile" class="col-md-4 control-label @if (!ModelState.IsValidField("mobile")) {<text>error-label</text>}">Mobile</label>
                    <div class="col-md-4">
                        <input id="mobile" name="mobile" placeholder="enter as 8005551212 (10 digits)" class="form-control input-md" type="text" @Validation.For("mobile")>
                        @Html.ValidationMessage("mobile")
                    </div>
                </div>

    Validation.RequireField("email", "You must enter your last name.");
        Validation.Add("email",
            Validator.Regex(@"^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$",
            "Invalid format for an email address")
            );
        Validation.RequireField("mobile", "You must specify a valid mobile number.");
        Validation.Add("mobile",
            Validator.Regex(@"(?<!\d)\d{10}(?!\d)", 
            "Not a valid 10 digit phone number")
            );

    Saturday, January 30, 2016 5:16 PM

Answers

  • User325035487 posted

    This is not from the C#. But your browser is doing html5 validation for email field. But cant understand mobile field. Better to use a JQuery Plugin like http://jqueryvalidation.org/rules

    if you want on tab out validation

    But when you submit the form. your c# validation kicks in

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, January 31, 2016 4:59 PM