Jump to content

Form Validation


Drezard

Recommended Posts

Hello. Now, I need a little help with some form validation. I have this form here:

 

<form action='' method='post'>
Username: <input type='text' name='user'><br>
Password: <input type='password' name='pass'><br>
<input type='submit' name='submit'>
</form>

 

So theres my HTML form. Can someone please give me an example using this code on how to validate a form. Now, I just want it that if the user doesn't enter any thing into either input boxes it throws back an error at the top of the page then shows the form again.

 

- Cheers, Daniel

Link to comment
Share on other sites

Well Ive Been Looking Around For You And Ive Found This:

Simple But Does The Job - And It Can Easily Be Changed To Suit Your Needs

Hope Ive Helped,

Tarun

 

 

<style>
.error {
color: red;
display:none;
}
</style>
  
<script>
function checkForm() {
user = document.getElementById("user").value;
pass = document.getElementById("pass").value;
  
if (user == "") {
hideAllErrors();
document.getElementById("userError").style.display = "inline";
document.getElementById("user").select();
document.getElementById("user").focus();
return false;
}else if (pass == "") {
hideAllErrors();
document.getElementById("passError").style.display = "inline";
document.getElementById("pass").select();
document.getElementById("pass").focus();
return false;
}
return true;
}

function hideAllErrors() {
document.getElementById("userError").style.display = "none"
document.getElementById("passError").style.display = "none"
}
</script>
  
<form onSubmit="return checkForm();" action='login.php' method='post'>
Username: <input type='text' name='user'><br>
<div class='error' id='userError'><br>Required: Please Enter Your Username<br></div><br>
Password: <input type='password' name='pass'><br>
<div class='error' id='passError'><br>Required: Please Enter Your Password<br></div><br>
<input type='submit' name='submit'>
</form>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.