Hi i am doing a form validation using javascript (not jquery) just plain javascript and i can not get this to work i shall paste my html and javascript code below if anyone can get this working for me it would be great
function validate() {
var name = document.getElementById('name').value;
var address = document.getElementById('add1').value;
var city = document.getElementById('city').value;
var postcode = document.getElementById('pcode').value;
var phonenum = document.getElementById('pnum').value;
var email = document.getElementById('email').value;
var conemail = document.getElementById('conemail').value;
var pword = document.getElementById('password').value;
var conpword = document.getElementById('conpassword').value;
if (name==""){
document.getElementById("name").style.backgroundColor = "red";
document.getElementById("name").innerHTML = "You havent entered a Name"
errorCount = errorCount + 1;
if (add1==""){
document.getElementById("add1").style.backgroundColor = "red";
document.getElementById("add1").innerHTML = "You havent entered a Address"
errorCount = errorCount + 1;
if (city==""){
document.getElementById("city").style.backgroundColor = "red";
document.getElementById("city").innerHTML = "You havent entered a City"
errorCount = errorCount + 1;
if (pcode<6){
document.getElementById("pcode").style.backgroundColor = "red";
document.getElementById("pcode").innerHTML = "You havent entered a vaild Postcode"
errorCount = errorCount + 1;
if (pnum<11){
document.getElementById("pnum").style.backgroundColor = "red";
document.getElementById("pnum").innerHTML = "You havent entered a vaild Phone Number"
errorCount = errorCount + 1;
if (email.indexOf("@")==-1){
document.getElementById("Email").innerHTML = "You havent entered a vaild email"
errorCount = errorCount + 1;
if (pword==""){
document.getElementById("pword").style.backgroundColor = "red";
document.getElementById("pword").innerHTML = "You havent entered a Password"
errorCount = errorCount + 1;
}
}
}
}
}
}
}
}
function checkemail() {
if(email.value == conemail.value){
document.getElementById("conemail").innerHTML = "emails dont match"
}
}
function checkpass(){
if(pword.value == conpword.value){
document.getElementById("conemail").innerHTML = "passwords dont match"
}
}
11-16-2012, 10:58 AM
coldfire421
As I have read your code, on you "Submit Details" button it says that it will call a Confirm function but I cannot see any confirm function in your javascript code
11-16-2012, 11:03 AM
coldfire421
Also you have too many incorrect syntax on your code, your "IF" statement are suppose to be a block statement
example
if(x > n){
// code here
}
if(y > x){
// code here
}
what you did is put opening curly braces in all "IF" statement and no close curly bracket at all then you put all close bracket at once after your last "IF" statement
11-16-2012, 11:08 AM
coldfire421
Another things is you have 2 functions named "checkemail" and "checkpass" and you put it inside your function "validate" and I have notice that you that 2 function is never been used
11-16-2012, 11:14 AM
coldfire421
Also this statement...
document.getElementById("name").innerHTML = "You havent entered a Name"
The text input has no innerHTML property. You can assign value to the text input using its value property, just like how you get the value of "name" variable from your text input "name"
This is the right...
document.getElementById('name').value = "You havent entered a Name";
and another things it is a good practice to declare the variable before you are going to use it. You have to declare the errorCount as zero before you append some value of it
11-16-2012, 02:21 PM
ianruk
Thank you so much for your help. i have changed some of the code as advised just making sure this is correct so here is my new code
function validate() {
var name = document.getElementById('name').value;
var address = document.getElementById('add1').value;
var city = document.getElementById('city').value;
var postcode = document.getElementById('pcode').value;
var phonenum = document.getElementById('pnum').value;
var email = document.getElementById('email').value;
var conemail = document.getElementById('conemail').value;
var pword = document.getElementById('password').value;
var conpword = document.getElementById('conpassword').value;
if (name==""){
document.getElementById("name").style.backgroundColor = "red";
document.getElementById('name').value = "You havent entered a Name";
errorCount = errorCount + 1;
}
if (address==""){
document.getElementById("add1").style.backgroundColor = "red";
document.getElementById("add1").value = "You havent entered a address";
errorCount = errorCount + 1;
}
if (city==""){
document.getElementById("city").style.backgroundColor = "red";
document.getElementById("city").value = "You havent entered a city";
errorCount = errorCount + 1;
}
if (postcode.length<6){
document.getElementById("pcode").style.backgroundColor = "red";
document.getElementById("pcode").value = "You havent entered a valid postcode";
errorCount = errorCount + 1;
}
if (phonenum.length<11){
document.getElementById("pnum").style.backgroundColor = "red";
document.getElementById("pnum").value = "You havent entered a valid phone number";
errorCount = errorCount + 1;
}
if (email.indexOf("@")==-1){
document.getElementById("Email").value = "You havent entered a valid email";
errorCount = errorCount + 1;
}
if(email.value == conemail.value){
document.getElementById("conemail").value = "your emails dont match";
errorCount = errorCount + 1;
}
if (pword==""){
document.getElementById("pword").style.backgroundColor = "red";
document.getElementById("pword").value = "You havent entered a password";
errorCount = errorCount + 1;
so i have a few more questions and i am so sorry for any bother i have caused. so my questions are:
1. how do i set the error count,so where n how would i set it to 0 and how would i make it work
2. the .value to bring up a message telling users they have either not entered the right info instead of having it displayed in the box is there a way to have it next to the box? or underneath the box?
3. how do i make all the errors come up onto the page at the same time? is that the error count?
4. last question the whole email and password dont match is that the right code?, also if i wanted the password and phone number to be a certain length is the code...
phonenum.length<11 correct? cause it doesnt seem to work
please if possible could you finish off and add anything else i am missing
thank you so much for your patience and help
11-20-2012, 11:45 AM
coldfire421
<!DOCTYPE html>
<html>
<head>
<title>JavaScript Form Validation</title>
<style type="text/css">
body{
font-family: Arial, Tahoma, Verdana;
font-size: 11px;
}
.errMsg{
color: #ff0000;
}
</style>
<script type="text/javascript">
var spanArray = null;
var form = null;