Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML/JavaScript: Simple form validation on submit

I'm trying to validate my form with the easiest way possible, but somehow it is not working and when I click submit it just takes me to the next page without giving the alert message:

HTML:

<form name="ff1" method="post" onsubmit="validateForm();">
    <input type="text" name="email" id="fremail" placeholder="[email protected]" />
    <input type="text" name="title" id="frtitle" placeholder="Title" />
    <input type="text" name="url"   id="frurl"   placeholder="http://yourwebsite.com/" />

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

JavaScript:

<script type="text/JavaScript">

function validateURL(url) {
    var reurl = /^(http[s]?:\/\/){0,1}(www\.){0,1}[a-zA-Z0-9\.\-]+\.[a-zA-Z]{2,5}[\.]{0,1}/;
    return re.test(url);
}

function validateForm()
{
    // Validate URL
    var url = $("#frurl").val();
    if (validateURL(url)) { } else {
        alert("Please enter a valid URL, remember including http://");
    }

    // Validate Title
    var title = $("#frtitle").val();
    if (title=="" || title==null) { } else {
        alert("Please enter only alphanumeric values for your advertisement title");
    }

    // Validate Email
    var email = $("#fremail").val();
    if ((/(.+)@(.+){2,}\.(.+){2,}/.test(email)) || email=="" || email==null) { } else {
        alert("Please enter a valid email");
    }
  return false;
}
</script>

Here is also in jsfiddle http://jsfiddle.net/CrLsR/

like image 258
Eduardo Go Avatar asked Apr 21 '13 18:04

Eduardo Go


People also ask

How do I validate a submit button?

The correct way is onclick="validate_activity();" and return false in validata_activity function, which will stop the default submit action.

How JavaScript form can be validated?

Basic Validation − First of all, the form must be checked to make sure all the mandatory fields are filled in. It would require just a loop through each field in the form and check for data. Data Format Validation − Secondly, the data that is entered must be checked for correct form and value.


3 Answers

You have several errors there.

First, you have to return a value from the function in the HTML markup: <form name="ff1" method="post" onsubmit="return validateForm();">

Second, in the JSFiddle, you place the code inside onLoad which and then the form won't recognize it - and last you have to return true from the function if all validation is a success - I fixed some issues in the update:

https://jsfiddle.net/mj68cq0b/

function validateURL(url) {     var reurl = /^(http[s]?:\/\/){0,1}(www\.){0,1}[a-zA-Z0-9\.\-]+\.[a-zA-Z]{2,5}[\.]{0,1}/;     return reurl.test(url); }  function validateForm() {     // Validate URL     var url = $("#frurl").val();     if (validateURL(url)) { } else {         alert("Please enter a valid URL, remember including http://");         return false;     }      // Validate Title     var title = $("#frtitle").val();     if (title=="" || title==null) {         alert("Please enter only alphanumeric values for your advertisement title");         return false;     }      // Validate Email     var email = $("#fremail").val();     if ((/(.+)@(.+){2,}\.(.+){2,}/.test(email)) || email=="" || email==null) { } else {         alert("Please enter a valid email");         return false;     }   return true; } 
like image 159
Adidi Avatar answered Oct 16 '22 05:10

Adidi


The simplest validation is as follows:

<form name="ff1" method="post">   <input type="email" name="email" id="fremail" placeholder="[email protected]" />   <input type="text" pattern="[a-z0-9. -]+" title="Please enter only alphanumeric characters." name="title" id="frtitle" placeholder="Title" />   <input type="url" name="url" id="frurl" placeholder="http://yourwebsite.com/" />   <input type="submit" name="Submit" value="Continue" /> </form>

It uses HTML5 attributes (like as pattern).

JavaScript: none.

like image 22
Niet the Dark Absol Avatar answered Oct 16 '22 04:10

Niet the Dark Absol


You need to return the validating function. Something like:

onsubmit="return validateForm();"

Then the validating function should return false on errors. If everything is OK return true. Remember that the server has to validate as well.

like image 35
Jensd Avatar answered Oct 16 '22 05:10

Jensd