Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 184
  • Last Modified:

Forms Validation

How do I validate a form on submit and print the error message on the Page instead of  alert box?

Thank you!!
0
cuconsortium
Asked:
cuconsortium
  • 7
  • 4
  • 4
2 Solutions
 
thecrewCommented:
I would suggest jQuery.  Easy to use, easy to customize.

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

0
 
cuconsortiumAuthor Commented:
why this one is so popular?  
0
 
cuconsortiumAuthor Commented:
I'm looking for java script.  how about something related to "getInnerHTML"?
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
thecrewCommented:
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
 
cuconsortiumAuthor Commented:
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
 
Michel PlungjanIT ExpertCommented:
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
 
cuconsortiumAuthor Commented:
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
 
thecrewCommented:
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
 
thecrewCommented:
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
 
Michel PlungjanIT ExpertCommented:
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
 
cuconsortiumAuthor Commented:
ok, i'm gonna try it out and report back~
0
 
cuconsortiumAuthor Commented:
Hi all~

  I was out for a week.  I'm gonna take a look at this this week.
0
 
Michel PlungjanIT ExpertCommented:
Happy?
0
 
cuconsortiumAuthor Commented:
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
 
Michel PlungjanIT ExpertCommented:
:)
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 7
  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now