Question

I have a javascript function that checks if someone entered their email:

function formValidate(){

    form1 = document.forms['_register1'];

    if (form1.elements['email'].value == 'Type Your Email Address Here') 

    {

        alert('Please enter your email address.');

        form1.elements['email'].focus();

        return false;

    }
}

Here is the form html:

<form method="post" enctype="multipart/form-data" name="_register1" id="_register" action="signup.php" >
<input type="text" class="field" value="Type Your Email Address Here" name="email" title="Type Your Email Address Here" />
<input type="submit" class="button button-signup" value="SIGN UP!" onclick="formValidate();" /> 

If the user has not entered an email address, I want the javascript alert to popup and once the user presses ok, I want the page to stay the same. I don't want the action="signup.php" to happen until the email is valid. This seems like it should be simple but I've looked all over the internet and can't find a solution.

Thanks.

Was it helpful?

Solution 2

I finally figured out the answer. The key is to use the javascript submit() function. Rather than use an input type submit, I use just a regular button. Then, after javascript validates the form and the form has the proper data, I used the submit function. Here is my code below. If anyone is curious, feel free to ask me about it.

function formValidate(){

    //define form1 as registration form
    form1 = document.forms['_register1'];
    var submission = true;
    //make sure email address is not blank
    if (form1.elements['email'].value == 'Type Your Email Address Here') {

        alert('Please enter your email address.');

        form1.elements['email'].focus();

        var submission = false;
    } 
    //submit form if data is good
    if(submission != false){
        form1.submit();
    }
}

And here is the html:

    <input type="text" value="Type Your Email Address Here" name="email" title="Enter email" />

    <input type="button" value="SIGN UP!" onclick="formValidation();" />    

OTHER TIPS

http://www.html-form-guide.com/php-form/php-login-form.html

this might be what you're looking for.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top