0

I am having some issues with JavaScript check my form for completion before submission. Here is my form code:

<form class="myform" accept-charset="UTF-8" onsubmit="return validateForm();" action="https://Autorespondercode.com" method="POST">

<div class="front-name"><input class="form-name" id="inf_field_FirstName" type="text" name="inf_field_FirstName" placeholder="First Name" /></div>


<div class="front-email"><input class="form-email" id="inf_field_Email" type="text" name="inf_field_Email" placeholder="Email" /></div>


<input style="background-color: #fc8f12;" type="submit" value="Subscribe" />


</form>

JavaScript:

function validateForm() { 
    var a=document.forms["myform"]["inf_field_FirstName"].value; 
    var b=document.forms["myform"]["inf_field_Email"].value; 
    if (a==null || a=="" || a=="First Name")
    {   alert("Please enter your First Name!");   
    return false;   }     
    var emailRegEx = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i;    
    if (document.myform.inf_field_Email.value.search(emailRegEx) == -1)  
    {           alert("Please enter a valid email address.");               
    return false;    } }

Appreciate any help/tips.

2
  • And... what is your problem that you encounter? Have you checked the console for errors thrown? Commented Nov 11, 2013 at 6:40
  • Are you using jQuery? Commented Nov 11, 2013 at 6:52

2 Answers 2

2

Replace your script with:

    function validateForm() {
        var a = document.getElementById('inf_field_FirstName').value;
        var b = document.getElementById('inf_field_Email').value;
        if (a == null || a == "" || a == "First Name") {
            alert("Please enter your First Name!");
            return false;
        }
        var emailRegEx = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i;
        if (document.getElementById('inf_field_Email').value.search(emailRegEx) == -1) {
            alert("Please enter a valid email address.");
            return false;
        }
    }

Browser is not able to get value with:

  var a=document.forms["myform"]["inf_field_FirstName"].value; 
  var b=document.forms["myform"]["inf_field_Email"].value; 
Sign up to request clarification or add additional context in comments.

1 Comment

Vicky, I think I am in love with you. I've been driving my head against a wall for hours trying to fix that thanks so much
0

Can you try like, You have not mentioned the form name myform in your form tag,

   <form name="myform" class="myform" accept-charset="UTF-8" onsubmit="return validateForm();" action="https://Autorespondercode.com" method="POST">

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.