Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8737497
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T10:32:09+00:00 2026-06-13T10:32:09+00:00

I am using HTML 5 validation and on several fields I am validating against

  • 0

I am using HTML 5 validation and on several fields I am validating against a pattern. Is it possible to find whether the type of validation error was for example a required error or a failure to match a pattern etc? Here is my code :

    <html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Contact</title>
</head>
    <body>
        @using (Html.BeginForm())
        {
            <div>
                <p>
                    @Html.LabelFor(model => model.Name)<br />
                    @Html.TextBoxFor(model => model.Name, new {@type = "text", @placeholder = "Enter a name", @required="true"})
                    @Html.ValidationMessageFor(model => model.Name)
                </p>

                <p>
                    @Html.LabelFor(model => model.EmailAddress)<br />
                    @Html.TextBoxFor(model => model.EmailAddress, new {@type = "text", @placeholder = "Enter an email address", @required="true", @pattern=@"^([a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?)$" })
                    @Html.ValidationMessageFor(model => model.EmailAddress)
                </p>

                <p>
                    @Html.LabelFor(model => model.PhoneNumber)<br />
                    @Html.TextBoxFor(model => model.PhoneNumber, new {@type = "text", @placeholder = "Enter a Phone Number", @required="true", @pattern=@"^[\+\d\(]{1}[\d\(]{1}[\d\-\(\) ]{3,19}$"})
                    @Html.ValidationMessageFor(model => model.PhoneNumber)
                </p>

                <p>
                    @Html.LabelFor(model => model.Details)<br />
                    @Html.TextAreaFor(model => model.Details, new {@type = "text", @placeholder = "Enter Details", @id = "Details", @required="true"})
                    @Html.ValidationMessageFor(model => model.Details)
                </p>
                <input type="submit" value="Submit" style="font-size:11px;font-family:Verdana;font-weight:bold;" />
            </div>
        }  
    </body>

    <script type="text/javascript">

        $(document).ready(function () {

            var elementsInput = document.getElementsByTagName("input");
            var elementsTextArea = document.getElementsByTagName("textarea");

            for (var i = 0; i < elementsInput.length; i++) {
                elementsInput[i].oninvalid = function (e) {
                    e.target.setCustomValidity("");
                    if (!e.target.validity.valid) {
                        switch (e.target.name) {
                            case "Name":
                                if ("@Thread.CurrentThread.CurrentUICulture.Name.ToLower()" == "fi-fi") {
                                    e.target.setCustomValidity("Anna nimesi");
                                } else {
                                    e.target.setCustomValidity("Please enter a Name");
                                }
                                break;
                            case "EmailAddress":
                                if ("@Thread.CurrentThread.CurrentUICulture.Name.ToLower()" == "fi-fi") {
                                    e.target.setCustomValidity("Anna sähköpostiosoitteesi");
                                } else {
                                    e.target.setCustomValidity("Please enter an Email Address");
                                }
                                break;
                            case "PhoneNumber":
                                if ("@Thread.CurrentThread.CurrentUICulture.Name.ToLower()" == "fi-fi") {
                                        e.target.setCustomValidity("Anna puhelinnumerosi");
                                    } else {
                                        e.target.setCustomValidity("Please enter a Phone Number");
                                    }
                                    break;
                            }
                        }
                };
                    elementsInput[i].oninput = function (e) {
                        e.target.setCustomValidity("");
                    };
                }

            for (var j = 0; j < elementsTextArea.length; j++) {
                elementsTextArea[j].oninvalid = function (e) {
                    e.target.setCustomValidity("");
                    if (!e.target.validity.valid) {
                        switch (e.target.name) {
                            case "Details":
                                if ("@Thread.CurrentThread.CurrentUICulture.Name.ToLower()" == "fi-fi") {
                                    e.target.setCustomValidity("Täytäthän yksityiskohdat");
                                } else {
                                    e.target.setCustomValidity("Please enter Details");
                                }
                                break;
                        }
                    }
                };
                elementsTextArea[j].oninput = function (e) {
                    e.target.setCustomValidity("");
                };
            }
        });

    </script>

</html>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T10:32:10+00:00Added an answer on June 13, 2026 at 10:32 am

    Figured this out, using element.validity.valueMissing and element.validity.patternMismatch (http://www.whatwg.org/specs/web-apps/current-work/multipage/association-of-controls-and-forms.html#the-constraint-validation-api)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Why this give error in W3c html validation ? I'm using HTML 4.01 Strict
I'm validating my website using the W3C HTML validation , and I'm getting errors
I have designed a webpage using HTML and client side validation using JavaScript.PHP for
I'm using (or trying to) JQuery Validation with WebForms/html. I have, basically (simplifying the
I am using curl like this: curl -s -F uploaded_file=@/path_to_file;type=text/html -F output=soap12 http://localhost/w3c-markup-validator/check >text.xml
In my Grails GSP file I'm using the HTML meta tag: <meta http-equiv=Content-Type content=text/html;
I am using MVC3 and added model validation with required attributes. Then I have
When implementing error-handling using the built-in validation-helpers on a strongly-typed view, you usually create
I am using MVC4. Validation is failing but validation error messages are not getting
What's the latest thinking on HTML validation when using a framework like Backbone or

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.