Solved

Forms Validation

Posted on 2011-02-14
15
178 Views
Last Modified: 2012-05-11
How do I validate a form on submit and print the error message on the Page instead of  alert box?

Thank you!!
0
Comment
Question by:cuconsortium
  • 7
  • 4
  • 4
15 Comments
 
LVL 3

Expert Comment

by:thecrew
ID: 34892973
I would suggest jQuery.  Easy to use, easy to customize.

http://docs.jquery.com/Plugins/validation

0
 

Author Comment

by:cuconsortium
ID: 34893034
why this one is so popular?  
0
 

Author Comment

by:cuconsortium
ID: 34893043
I'm looking for java script.  how about something related to "getInnerHTML"?
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 3

Expert Comment

by:thecrew
ID: 34893103
jQuery is JavaScript.

It's popular because it works, it's open, it's free, it's easy to use and it's well documented.

Read the page I sent you. It will do EXACTLY what you are asking to do.
0
 

Author Comment

by:cuconsortium
ID: 34893448
it seems the validation is pretty fix to E-mail Address, Phone Number, URL... and etc.  I need to valdidate the input types in my ownways.
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 34894384
Here is the canonical validation. jQuery is not needed for simple validation and I only use jQuery if I already need it for some animation.
<html>
<head>
<script>
function validate(theForm) {
  var field1 = theForm.field1.value;
  if (field1=="") {
    alert('Please enter something in field1');
    theForm.field1.focus(); // put the cursor back in the field
    return false; // cancel submission
  }

  var field2 = theForm.field2.value;
  if (field2=="") {
    alert('Please enter something in field2');
    theForm.field2.focus(); // put the cursor back in the field
    return false; // cancel submission
  }

// repeat the above as needed

  return true; // here all fields validates, allow submission
}
window.onload=function() {
  // courtesy method to place the cursor in the first field
  document.forms[0].field1.focus(); // forms[0] is the first form on the page
}
</script>
</head>
<body>
<form onsubmit="return validate(this)">
<input type="text" name="field1" value="" /><br>
<input type="text" name="field2" value="" /><br>
<input type="submit" />
</form>
</body>
</html>

Open in new window

0
 

Author Comment

by:cuconsortium
ID: 34899992
mplungjan,

  it's pretty good, but it's prompting alert box.  Is there anyway that i can print the error message on the page itself?

  Or

   have one alert box list all the errors when onSumbit?

Thank you!!
0
 
LVL 3

Expert Comment

by:thecrew
ID: 34900356
That is the reason I was suggesting jQuery, you can easily show errors inline wherever and however you like. The validation can be applied to anything you want (not just emails and phone numbers, those are just examples on the plugin page). I've used it to validate a date-range, a percentage, all sorts of crazy things. Although I completely agree with mplungjan, if you are not already using it for another legitimate reason it might not be the best solution.

To change text on the page you could use

document.getElementById('theID').innerHTML = 'Error man!';
(your HTML could be something like this: <b id="theID"></b>)


0
 
LVL 3

Assisted Solution

by:thecrew
thecrew earned 150 total points
ID: 34900458
Here you go, I took mplungjan's code and changed a few things. Now you won't get the alert boxes, instead you will get inline text error messages, the messages will automatically clear when you submit too.

<html>
<head>
<script>
function validate(theForm) {
  var field1 = theForm.field1.value;
  if (field1=="") {
    document.getElementById('firstError').innerHTML = 'Error! Missing this input!';
	theForm.field1.focus(); // put the cursor back in the field
	var error = "error"; // set error var so we can stop the from from submitting
  } else { // if the field has stuff in it then make sure the error is blank
  	document.getElementById('firstError').innerHTML = '';
  }

  var field2 = theForm.field2.value;
  if (field2=="") {
    document.getElementById('secondError').innerHTML = 'Error! Missing this input!';
    theForm.field2.focus(); // put the cursor back in the field
	var error = "error";
  } else {
  	document.getElementById('secondError').innerHTML = '';
  }

// repeat the above as needed

  if (error == "error"){
  	return false; // if there was an error anywhere then cancel submission
  }

  return true; // here all fields validates, allow submission
}
window.onload=function() {
  // courtesy method to place the cursor in the first field
  document.forms[0].field1.focus(); // forms[0] is the first form on the page
}
</script>
</head>
<body>
<form onsubmit="return validate(this)">
<input type="text" name="field1" value="" />
<b id="firstError"></b><br>
<input type="text" name="field2" value="" />
<b id="secondError"></b><br>
<input type="submit" />
</form>
</body>
</html>

Open in new window

0
 
LVL 75

Accepted Solution

by:
Michel Plungjan earned 350 total points
ID: 34900913
I could do that myself ;)

Here is a more elegant version
If you have a message, the field is obligatory.
Let me know if you need selects and/or radios too
<html>
<head>
<style>
.error { color:red }
</style>
<script>
var messages = {
	"field1":"Please enter a value in field1",
	"field2":"Please enter a value in field2"
} // notice no comma after the last item	
	
function validate(theForm) {
  var fieldToFocus=null;
  for (var o in messages) {
    var field = theForm.elements[o];
    var error = document.getElementById(o+"error");
    error.style.visibility='hidden'; // keep the space
    if (field.value=="") {
      error.innerHTML = messages[o];
      error.style.visibility="visible"
      if (!fieldToFocus) fieldToFocus=field;
    }
  }

  if (fieldToFocus){
  	fieldToFocus.focus();
  	return false; // if there was an error anywhere then cancel submission
  }

  return true; // here all fields validates, allow submission
}
window.onload=function() {
  // courtesy method to place the cursor in the first field
  document.forms[0].field1.focus(); // forms[0] is the first form on the page
}
</script>
</head>
<body>
<form onsubmit="return validate(this)">
<input type="text" name="field1" value="" />&nbsp;<span id="field1error" class="error"></span><br />
<input type="text" name="field2" value="" />&nbsp;<span id="field2error" class="error"></span><br />
<input type="submit" />
</form>
</body>
</html>

Open in new window

0
 

Author Comment

by:cuconsortium
ID: 34919581
ok, i'm gonna try it out and report back~
0
 

Author Comment

by:cuconsortium
ID: 34999400
Hi all~

  I was out for a week.  I'm gonna take a look at this this week.
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 36178104
Happy?
0
 

Author Comment

by:cuconsortium
ID: 36182147
yesss!!! good job~~!  I feel bad that I wasn't able to be more involved than I want myself to be.  
You've done a good job!!!
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 36182243
:)
0

Featured Post

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Hide Table in merge 3 31
Is there a way to bind the ID to the text value of the search result? 28 51
on window close, not beforeunload 6 17
Wordpress Query 5 25
Batch, VBS, and scripts in general are incredibly useful for repetitive tasks.  Some tasks can take a while to complete and it can be annoying to check back only to discover that your script finished 5 minutes ago.  Some scripts may complete nearly …
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

839 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