Hey guys, trying to teach myself a bit of javascript as i would like to get a website up and running. Please have a look at the code below. I'm trying to create input for a webpage. In the first piece of code, i'm declaring all the functions(which i'm sure are wrong) and i'm also attempting to validate them
The 2nd piece of code contains the area where i'm calling them. As you can probably see, the code is a bit rough. Most of the info i've pieced together from reading previous answers on this forum.
I realise there are probably many errors here but if someone can start me in the right direction with whats wrong, i'd be happy with that.
Thanks in advance.
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
function validateAll(myForm){
validateNoOfDigits1(myForm.Firstname);
validateNoOfDigits1(myForm.Surname);
validateNoOfDigits2(myForm.Age);
validateAge(myForm.Age);
validateNotEmpty(myForm.Username);
validatePassword(myForm.pass1);
validateNoOfDigits3(myForm.pass1);
validatePasswordEqual(myForm.pass2);
validateNoOfDigits3(myForm.pass2);
}
function validateNotEmpty(value){
if (value==null || value==""){
alert("Field cannot be empty");
return false;
}
return true;
}
function validateAge(value){
checkvalue=parseFloat(value);
if (value<18){
var txt = "Applicants must be over 18";
alert(txt);
return false;
}
return true;
}
function validateNoOfDigits1(value){
if (value.length<=1){
alert("Incorrect input. Please enter name correctly.");
return false;
}
return true;
}
function validateNoOfDigits2(value){
checkvalue=parseInt(value);
if (value.length<=0 || value.length<2){
var txt = "Incorrect input. Please enter correct age.";
alert(txt);
return false;
}
return true;
}
function validateNoOfDigits3(value){
if (value.length<8){
alert("Incorrect Password Length. Please enter password with min 8 characters");
return false;
}
return true;
}
function validatePasswordEqual(value){
if (value != Password.name){
alert("Incorrect Password. Double check passwords match");
return false;
}
return true;
}
function validatePassword(value) {
var error = "";
var illegalChars = /[\W_]/; // allow only letters and numbers
if (value == "") {
error = "You didn't enter a password.\n";
} else if (value.length > 8) {
error = "The password is the wrong length. \n";
} else if (illegalChars.test(value)) {
error = "The password contains illegal characters.\n";
} else if (!((value.search(/(a-z)+/)) && (value.search(/(0-9)+/)))) {
error = "The password must contain at least one numeral.\n";
}
return error;
}
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
<script language ="Javascript" src="js/Function2.js"></script>
<link rel = "stylesheet" type = "text/css" href = "css/index.css" />
</head>
<body>
<form onsubmit="validateAll(this)">
Firstname: <input type="text" name="Firstname"/><br/>
Surname: <input type="text" name="Surname"/><br/>
Age: <input type="text" name="Age"/><br/>
Username: <input type="text" name="Username"/><br/>
Password: (number and letters, min 8 characters):
<input type="text" name="pass1"/> <br/>
Repeat Password:
<input type="text" name="pass2"/> <br/>
<input type="submit" value="Submit Details" name="Button1"/>
</form>
</body>
</html>