0

On this form when I enter the email address correctly and leaves the name field as a blank, the page is submitting and the javacript not running. Otherwise its working fine.

Please someone help me to solve this JavaScript.

function validateform(data)
{
    var validField = "";
    var namevalid=/^[a-zA-Z ]+$/;
    if(data.name.value == ""){validField += "- Please Enter Your Name\n";}
    else if(data.name.value.search(namevalid)==-1){validField += "- Entered Name contains Numbers or Symbols\n";}
    if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(data.email.value)){
        return true;
        }
        validField += "- Please Enter A Valid E-mail Address\n";
        alert(validField);
        return false;
}


<form name="data" onsubmit="return validateform(this);" action="some.php" method="post">
<div>
<label>Name:</label><input type="text" id="name" name="name" size="20"  maxlength="20"/></div>

<div>
<label>E- mail Address:</label><input type="text" id="email" name="email" size="20"  maxlength="30"/></div>

<input type="submit" name="submit" value="Start Now!"/>
</form>

5 Answers 5

1

You need little change in your code. Problem is because function validateform() always returning false irrespective of email validation. Test below working page along with your code. You should add alert and return false in else {} block.

<html> 
<head>  
<script type="text/javascript">
function validateform(data)
{
    var validField = "";
    var namevalid=/^[a-zA-Z ]+$/;
    if(data.name.value == ""){validField += "- Please Enter Your Name\n";}
    else if(data.name.value.search(namevalid)==-1){validField += "- Entered Name contains Numbers or Symbols\n";}
    if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(data.email.value)){
        return true;
     }
     else {
        validField += "- Please Enter A Valid E-mail Address\n";
        alert(validField);
        return false;
        }
}
</script> 
</head>
<body>
<form name="data" onsubmit="return validateform(this);" action="some.php" method="post">
<div>
<label>Name:</label><input type="text" id="name" name="name" size="20"  maxlength="20"/></div>
<div>
<label>E- mail Address:</label><input type="text" id="email" name="email" size="20"  maxlength="30"/></div>

<input type="submit" name="submit" value="Start Now!"/>
</form>
</body>
</html>
Sign up to request clarification or add additional context in comments.

Comments

0

You need to refactor your code a little bit. At the minute if the email address is valid your return true out of the function regardless of the name field.

function validateform(data) {
    var validField = "";
    var isValid = false;
    var namevalid = /^[a-zA-Z ]+$/;
    if (data.name.value == "") {
        validField += "- Please Enter Your Name\n";
    }
    else if (data.name.value.search(namevalid) == -1) { 
        validField += "- Entered Name contains Numbers or Symbols\n"; 
        }
    if (!(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(data.email.value))) {
        validField += "- Please Enter A Valid E-mail Address\n";
    };
    if (validField == "") {
        isValid = true;
    }
    if (!isValid) {
        alert(validField);
    }
    return isValid;
} 

Comments

0

You need to place your function between <script> tags, other than that this looks ok to me.

1 Comment

He probably posted a snippet, otherwise he would see all the JS on the screen and know something is wrong.
0

I'm not quite sure why this is failing, but the function isn't called at all. Might be the onsubmit.

Why don't you use a library like jQuery? Have a look at this example, this works fine: http://jsfiddle.net/tR3XQ/1/

$("form[name=data]").submit(function(){

    var validField = "";
    var namevalid=/^[a-zA-Z ]+$/;
    if(data.name.value == ""){validField += "- Please Enter Your Name\n";}
    else if(data.name.value.search(namevalid)==-1){validField += "- Entered Name contains Numbers or Symbols\n";}
    if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(data.email.value)){
        return true;
    }
    validField += "- Please Enter A Valid E-mail Address\n";
    alert(validField);
    return false;

});​

Comments

0

You need to add a check for a value in validField for the email check otherwise it is ignored

if (validField.length = 0 && /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(data.email.value)){
  return true;
}
validField += "- Please Enter A Valid E-mail Address\n";
alert(validField);
return false;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.