Javascript: Form Validation - Remove error messages from corrected fields - Stack Overflow

when I submit a form with errors (for example user has not pleted a required field) I am able to displa

when I submit a form with errors (for example user has not pleted a required field) I am able to display the error messages. However, If I correct one of the fields and submit again BOTH error messages remain.

The errors only go when ALL fields are correctly filled in.

What have I done wrong?

If i correct a field and click submit I eant the error message for the corrected field to go away and only the remining incorrect fields to have error messages.

HTML:

   <form id="frm1">
   <fieldset id="controls">
    <div>
      <label for="firstname">First Name: </label>
       <input type="text" id="firstname">
        <span id="errFname" class="errmsg">&#42 You must enter a first name</span>
    </div>
    <div>
       <label for="lastname">Last Name: </label>
       <input type="text" id="lastname">
        <span id="errLname" class="errmsg">&#42 You must enter a first name</span>
    </div>
    <div>
      <input type="submit" value="Submit">
    </div>
    </fieldset>
   </form>

SCRIPT:

function checkForm(){



    document.getElementById("frm1").addEventListener("submit", function(e) {

        var errors = [];


        //Validate first name: Required, Alphabetic (no numbers)
        if(document.getElementById("firstname").value === "") {

            document.getElementById("errFname").style.display = "inline";
            document.getElementById("firstname").focus();

            errors.push("required");

        }//close if


        var alphaRegEx = /^[a-zA-Z]+$/;;
        var alphafname = document.getElementById("firstname").value;
        var alphalname = document.getElementById("lastname").value;

        //check if first name has any digits
        if (!alphaRegEx.test(alphafname)){

            document.getElementById("errFname").style.display = "inline";
            document.getElementById("firstname").value="";
            document.getElementById("firstname").focus();

            errors.push("numeric");


        }//close if

        //check if last name has any digits
        if (!alphaRegEx.test(alphalname)){

            document.getElementById("errLname").style.display = "inline";
            document.getElementById("lastname").value="";
            document.getElementById("lastname").focus();

            errors.push("numeric");


        }//close if

        //If you want, you can do something with your errors, if not, just return
        if (errors.length > 0) {
            e.preventDefault();
            return false;
        }
        return true;

    });//close function



}//close function (checkForm)


window.onload=checkForm;

when I submit a form with errors (for example user has not pleted a required field) I am able to display the error messages. However, If I correct one of the fields and submit again BOTH error messages remain.

The errors only go when ALL fields are correctly filled in.

What have I done wrong?

If i correct a field and click submit I eant the error message for the corrected field to go away and only the remining incorrect fields to have error messages.

HTML:

   <form id="frm1">
   <fieldset id="controls">
    <div>
      <label for="firstname">First Name: </label>
       <input type="text" id="firstname">
        <span id="errFname" class="errmsg">&#42 You must enter a first name</span>
    </div>
    <div>
       <label for="lastname">Last Name: </label>
       <input type="text" id="lastname">
        <span id="errLname" class="errmsg">&#42 You must enter a first name</span>
    </div>
    <div>
      <input type="submit" value="Submit">
    </div>
    </fieldset>
   </form>

SCRIPT:

function checkForm(){



    document.getElementById("frm1").addEventListener("submit", function(e) {

        var errors = [];


        //Validate first name: Required, Alphabetic (no numbers)
        if(document.getElementById("firstname").value === "") {

            document.getElementById("errFname").style.display = "inline";
            document.getElementById("firstname").focus();

            errors.push("required");

        }//close if


        var alphaRegEx = /^[a-zA-Z]+$/;;
        var alphafname = document.getElementById("firstname").value;
        var alphalname = document.getElementById("lastname").value;

        //check if first name has any digits
        if (!alphaRegEx.test(alphafname)){

            document.getElementById("errFname").style.display = "inline";
            document.getElementById("firstname").value="";
            document.getElementById("firstname").focus();

            errors.push("numeric");


        }//close if

        //check if last name has any digits
        if (!alphaRegEx.test(alphalname)){

            document.getElementById("errLname").style.display = "inline";
            document.getElementById("lastname").value="";
            document.getElementById("lastname").focus();

            errors.push("numeric");


        }//close if

        //If you want, you can do something with your errors, if not, just return
        if (errors.length > 0) {
            e.preventDefault();
            return false;
        }
        return true;

    });//close function



}//close function (checkForm)


window.onload=checkForm;
Share Improve this question edited Jul 21, 2017 at 18:20 Cœur 38.8k25 gold badges206 silver badges279 bronze badges asked Jul 20, 2015 at 10:28 Maurice GreenlandMaurice Greenland 3152 gold badges5 silver badges20 bronze badges 2
  • 1 Your alphaRegEx is wrong, it validates only first symbol, regex should be /^[a-zA-Z]+$/ – sbedulin Commented Jul 20, 2015 at 10:38
  • You really need to stop ment the closing braces with "close if", "close function" etc. as they are useless. – Viktor Bahtev Commented Jul 20, 2015 at 11:34
Add a ment  | 

2 Answers 2

Reset to default 2

You must first clear all the errors when the checkForm is called.

function checkForm() {
    clearErrors();

    Rest functionality same...
}

function clearErrors() {
    Array.prototype.forEach.call(
        document.getElementsByClassName("errmsg"),
        function(el) { el.style.display = "none"; }
    );
}

This will clear all the errors first and then display only those errors which have not yet been resolved.

The solution was set each error message display property to 'none' before going through the validation.

function checkForm() {

        //Event listener
        document.getElementById("contactForm").addEventListener("submit", function(prevent) {

//Set display property for inline error messages to none
            document.getElementById("errFname").style.display = 'none';
            document.getElementById("errLname").style.display = 'none';

//Rest of code remains the same...

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745487893a4629849.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信