Solved

Simple form required field validation.

Posted on 2011-02-12
4
320 Views
Last Modified: 2012-05-11
Hi,

Im trying to find a simple script to let me validate 4 required fields in my form.
If one of them is empty an alert box should display which fields are missing without letting the form submit.

The form has been built. It submits to a PHP script fine.

I cant find a simple validation script that I can plug into it. They all seem over complicated.

Does anyone have anything ready to plugin?

Thanks for your help
0
Comment
Question by:SM17CH
  • 2
4 Comments
 
LVL 9

Accepted Solution

by:
xav056 earned 200 total points
ID: 34881228
0
 
LVL 75

Assisted Solution

by:Michel Plungjan
Michel Plungjan earned 300 total points
ID: 34881559
Poor example.
They do not even pass the form to the function

Here is the canonical way to validate a form - it will work on all browsers since the beginning of time
It would have been useful to see your form code however. In this case I will have to guess at the field names and not handle radio/checkboxes
<html>
<head>
<script>
function validate(theForm) { // passing the form itself to the function
  var firstName=theForm.firstName.value;
  if (firstName=="") {
    alert("Please enter your First Name");
    theForm.firstName.focus(); // return the cursor to the field
    return false; // cancel the submission
  }

  var lastName=theForm.lastName.value;
  if (lastName=="") {
    alert("Please enter your Last Name");
    theForm.lastName.focus(); // return the cursor to the field
    return false; // cancel the submission
  }

  // repeat the above for each field
  
  return true; // when we get to here, all fields validates, allow submission
}

// the following is a courtesy function to place the cursor in the first field 
window.onload=function() {
  document.forms[0].firstName.focus(); // forms[0] is the first form on the page
}
</script>
</head>
<body>
<form onsubmit="return validate(this)">
<input type="text" name="firstName" value="" />
<input type="text" name="lastName" value="" /><br />
<input type="submit" />
</form>
</body>
</html>

Open in new window

0
 
LVL 2

Author Comment

by:SM17CH
ID: 34881666
Yep that works,

I ended up doing a simlar thing from another tutorial I found.

<script type="text/javascript">
function validateForm()
{
var x=document.forms["booking"]["First-Name"].value
var y=document.forms["booking"]["Last-Name"].value
var z=document.forms["booking"]["Phone-Number"].value
var w=document.forms["booking"]["Suburb"].value
if (x==null || x=="" || y==null || y=="" || z==null || z=="" || w==null || w=="")
  {
  alert("Please complete all required fields (*)");
  return false;
  }
 
}
</script>

Open in new window

0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 34881741
I will argue that my suggestion is more user-friendly and does not rely on the form being named.
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Form submit issue 11 54
Importing and exporting data Magento 1.x ? 4 37
angularjs controller not found 1 18
angularls and plnkr 14 18
Shoutout to Emily Plummer (http://www.experts-exchange.com/members/eplummer26.html) for giving me this article! She did most of it, I just finished it up and posted it for her :)    Introduction In a previous article (http://www.experts-exchang…
Styling your websites can become very complex. Here I'll show how SASS can help you better organize, maintain and reuse your CSS code.
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now