skip to Main Content

Here is my JS code for form validation, It doesn’t validate the condition for the name that i entered, even I’ve given the condition to validate the name; it doesn’t work on it and if i didn’t enter the name and it click on submit, it alerts enter valid email address.

<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Sri Eshwar Conference Registration Form</title>
    <link rel="stylesheet" href="MyStyle.css">

</head>
<body style="font-family: bahnschrift, sans-serif;font-size: large;">
    <center><br>
        <h1><b>SRI ESHWAR CONFERENCE<br>REGISTRATION FORM</b></h1><br>
        <form name="RegForm" onsubmit="return validateForm()" method="get" enctype="multipart/form-data">
        <table>
            <tr>
                <th><label for="name">Name : </label></th>
                <td><input autofocus name="name" id="name" placeholder="Enter Your Name" required ></td>
            </tr>
            <tr>
                <th><label for="email">Email : </label></th>
                <td><input name="Email" id="email" type="email" required placeholder="Enter email here"></td>
            </tr>
            <tr>
                <th><label for="code" >Country Code : </label></th>
                <td><input name="country code" id="code" type="text" maxlength="7" value="+91" required></td>
            </tr>
</body>
<script>
function validateForm() {
    var name = document.forms["RegForm"]["name"].value;
    var email = document.forms["RegForm"]["email"].value;
if (name == " ") {
    alert("Please enter a valid name.");
    return false;
}

if (email == "") {
    alert("Please enter a valid email address.");
    return false;
}
return true;
}

    function Redirect() {
    window.open("result.html", "_blank");
    }

I’ve tried adding space inside in the condition if name == "(space)", but it is not working. I’m a beginner in Web development, So notify and teach my about this 😅

2

Answers


  1. In your name == " " you put a space instead of a blank string.
    This is how it should look:

    
    if (name = ""){
    // code
    }
    
    
    Login or Signup to reply.
  2. An onsubmit function only works when a submit button is clicked. So you need to add a submit button inside the form.

    `<form name="RegForm" onsubmit="return validateForm()" method="get" enctype="multipart/form-data">
            <table>
                <tr>
                    <th><label for="name">Name : </label></th>
                    <td><input autofocus name="name" id="name" placeholder="Enter Your Name" required></td>
                </tr>
                <tr>
                    <th><label for="email">Email : </label></th>
                    <td><input name="Email" id="email" type="email" required placeholder="Enter email here"></td>
                </tr>
                <tr>
                    <td><button type="submit">submit</button></td>
                </tr>
            </table>
        </form>`
    

    If you want to use the button outside the form, you have to assign the form "id" in the "form" attribute of the submit button.

    `<form id="myform" method="get" action="something.php">
       <input type="text" name="name" />
    </form>
    <input type="submit" form="myform" value="Update"/>`
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search