How to make a JavaScript form validation that displays an error message simultaneously

Favouronu

Am a new JavaScript programmer and am trying to make a form validator using JavaScript but the error messages don't seem to be displaying if all the forms are not filled, that is if all are empty, only the name form error displays.

This is what I tried:

function myValidate() {
    var x = document.forms["myform"]["name"].value;
    var y = document.forms["myform"]["country"].value;
    var z = document.forms["myform"]["occupation"].value;
    var a = document.forms["myform"]["status"].value;

    if (x == "null" || x == "") {
        var b = document.getElementById("nameErr");
        b.innerHTML = "Name Must Be Filled Out";
        return false;
    } else {
        var b = document.getElementById("nameErr");
        b.innerHTML = "";
        return true;
    }

    if (y == "null" || y == "") {
        var c = document.getElementById("countryErr");
        c.innerHTML = "Country Must Be Filled Out";
        return false;
    } else {
        var c = document.getElementById("countryErr");
        c.innerHTML = "";
        return true;
    }

    if (z == "null" || z == "") {
        var d = document.getElementById("occupationErr");
        d.innerHTML = "Occupation Must Be Filled Out";
        return false;
    } else {
        var d = document.getElementById("occupationErr");
        d.innerHTML = "";
        return true;
    }

    if (a == "null" || a == "") {
        var e = document.getElementById("statusErr");
        e.innerHTML = "Status Must Be Filled Out";
        return false;
    } else {
        var e = document.getElementById("statusErr");
        e.innerHTML = "";
        return true;
    }
}

This is the JavaScript code.

<form action="process.php" method="post" onsubmit="return myValidate()" name="myform">
    Name:
    <input type="text" id="name" name="name">
    <span id="nameErr"></span><br><br>

    Country:
    <input type="text" id="country" name="country">
    <span id="countryErr"></span><br><br>

    Occupation:
    <input type="text" id="occupation" name="occupation">
    <span id="occupationErr"></span><br><br>

    Status:
    <input type="text" id="status" name="status">
    <span id="statusErr"></span><br><br>

    <input type="submit" name="submit" value="Submit">
</form>

This is the HTML form.

Please help, Thanks

Dutchie432

It seems you don't realize that calling return ends the function you are in. If the validation fails, you'll want to return false, but you do not want to return true until the end of the validation process.

Try:

function myValidate() {
    var x = document.forms["myform"]["name"].value;
    var y = document.forms["myform"]["country"].value;
    var z = document.forms["myform"]["occupation"].value;
    var a = document.forms["myform"]["status"].value;
    var b = document.getElementById("nameErr");    
    var c = document.getElementById("countryErr");
    var d = document.getElementById("occupationErr");
    var e = document.getElementById("statusErr");

    if (!x || x.length==0) {
        b.innerHTML = "Name Must Be Filled Out";
        return false;
    } else {
        b.innerHTML = "";
    }

    if (!y || y.length==0) {
        c.innerHTML = "Country Must Be Filled Out";
        return false;
    } else {
        c.innerHTML = "";
    }

    if (!z || z.length==0) {
        d.innerHTML = "Occupation Must Be Filled Out";
        return false;
    } else {
        d.innerHTML = "";
    }

    if (!a || a.length==0) {
        e.innerHTML = "Status Must Be Filled Out";
        return false;
    } else {
        e.innerHTML = "";
    }
    return true;
}

This will return the first error it comes across. If you want to check them all at once, you will need to store your true/false in a variable and hold off on any return calls until then end and call return theVariableName;

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Error message not working in simple javascript form validation

From Dev

multiple form validation error message in single alert message javascript

From Dev

How to make a python program that lists the positions and displays and error message if not found

From Dev

error message in jquery form validation

From Dev

Form validation - Error Message not output

From Dev

How to give custom field name in laravel form validation error message

From Dev

AngularJS custom form validation error message

From Dev

Given error message is not displayed in Spring Form validation

From Dev

Form validation with error message using angular ngMessage

From Dev

AngularJS custom form validation error message

From Dev

Form validation error message is not shown in ModelForm

From Dev

Given error message is not displayed in Spring Form validation

From Dev

angularJs Form validation with dynamic error message

From Dev

How to make a form validation in InertiaJS

From Dev

Javascript Form Validation - jsFiddle Error

From Dev

javascript form validaton, Validation message Disappear Quickly

From Dev

How to get full form validation error message without form helper in Play Framework 2?

From Dev

How to get full form validation error message without form helper in Play Framework 2?

From Dev

Error message show before the form submit in angularjs form validation

From Dev

Error message show before the form submit in angularjs form validation

From Dev

How to do Form Validation in Javascript

From Dev

Javascript validation - Error message only flashes for a second

From Dev

Javascript validation - Error message only flashes for a second

From Dev

validation form : how to show error message all at one time for the blank input instead of one by one?

From Dev

validation form : how to show error message all at one time for the blank input instead of one by one?

From Dev

How to show error message in the form

From Dev

How to make Coding Simple Request Form Validation

From Dev

JavaScript Validation form did not stop on error

From Dev

javascript form validation_error in if condition

Related Related

  1. 1

    Error message not working in simple javascript form validation

  2. 2

    multiple form validation error message in single alert message javascript

  3. 3

    How to make a python program that lists the positions and displays and error message if not found

  4. 4

    error message in jquery form validation

  5. 5

    Form validation - Error Message not output

  6. 6

    How to give custom field name in laravel form validation error message

  7. 7

    AngularJS custom form validation error message

  8. 8

    Given error message is not displayed in Spring Form validation

  9. 9

    Form validation with error message using angular ngMessage

  10. 10

    AngularJS custom form validation error message

  11. 11

    Form validation error message is not shown in ModelForm

  12. 12

    Given error message is not displayed in Spring Form validation

  13. 13

    angularJs Form validation with dynamic error message

  14. 14

    How to make a form validation in InertiaJS

  15. 15

    Javascript Form Validation - jsFiddle Error

  16. 16

    javascript form validaton, Validation message Disappear Quickly

  17. 17

    How to get full form validation error message without form helper in Play Framework 2?

  18. 18

    How to get full form validation error message without form helper in Play Framework 2?

  19. 19

    Error message show before the form submit in angularjs form validation

  20. 20

    Error message show before the form submit in angularjs form validation

  21. 21

    How to do Form Validation in Javascript

  22. 22

    Javascript validation - Error message only flashes for a second

  23. 23

    Javascript validation - Error message only flashes for a second

  24. 24

    validation form : how to show error message all at one time for the blank input instead of one by one?

  25. 25

    validation form : how to show error message all at one time for the blank input instead of one by one?

  26. 26

    How to show error message in the form

  27. 27

    How to make Coding Simple Request Form Validation

  28. 28

    JavaScript Validation form did not stop on error

  29. 29

    javascript form validation_error in if condition

HotTag

Archive