Reputation: 119
getting undefined as a return type for the method validateHallticket please check the code & modify accordingly so, that when i click the submit button i should able to get the appropriate return types.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>4Cubes Site</title>
<script type="text/javascript">
function validateForm(form) {
document.writeln(validateNames(form["firstname"]));
document.writeln(validateHallticket(form["hallticket"])); // getting undefined value
if (validateNames(form["firstname"]) && validateHallticket(form["hallticket"])) {
form.sub`enter code here`mit();
}
else {
alert("Please Fill the required Fields");
}
}
function validateNames(inputField) {
Names_help = document.getElementById('lastname_help');
if (inputField.value.length == 0) {
if (Names_help!= null) {
Names_help.innerHTML = "Please Enter a validate Name";
return false;
}
}
else {
Names_help.innerHTML = "";
return true;
}
}
function validateHallticket(inputField) {
var regex = /^\d{2}K91A\d{4}$/;
var rs = regex.test(inputField.value);
hallticket_help = document.getElementById('hallticket_help');
if (!regex.test(inputField.value)) {
if (hallticket_help != null) {
hallticket_help.innerHTML = "Enter a valid hallticket";
return false;
}
}
else {
hallticket_help.innerHTML = "";
return true;
}
}
</script>
</head>
<body>
<center>
<font face="Arabic Transparent" size="6" color="Teal">4cUBeS College</font>
</center>
<br></br>
<br></br>
<form method="post" action="servlet.do" name="myform">
HallTicket:
<input type="text" name="hallticket" id="hallticket"
onblur="validateHallticket(this)"></input>
<span id="hallticket_help" style="color:Red; font-style:italic;"> </span>
<br></br>
FirstName:
<input type="text" name="firstname" id="firstname"
onblur="validateNames(this)"></input>
<span id="firstname_help" style="color:Red; font-style:italic;"> </span>
<br></br>
LastName:
<input type="text" name="lastname" id="lastname"
onblur="validateNames(this)"></input>
<span id="lastname_help" style="font-style:italic; color:Red;"> </span>
<center>
<input type="button" value="SUBMIT" onclick="validateForm(this.form);"></input>
</center>
</form>
</body>
</html>
Upvotes: 0
Views: 76
Reputation: 167162
This is because, the function loads before the DOM loads. Try moving the <script>
tag just before </body>
and it works.
Notes
return false;
when invalid.onsubmit
event of the <form>
tag.<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>4Cubes Site</title>
</head>
<body>
<center>
<font face="Arabic Transparent" size="6" color="Teal">4cUBeS College</font>
</center>
<br></br>
<br></br>
<form method="post" action="servlet.do" name="myform" onsubmit="return false;">
HallTicket:
<input type="text" name="hallticket" id="hallticket"
onblur="validateHallticket(this)"></input>
<span id="hallticket_help" style="color:Red; font-style:italic;"> </span>
<br></br>
FirstName:
<input type="text" name="firstname" id="firstname"
onblur="validateNames(this)"></input>
<span id="firstname_help" style="color:Red; font-style:italic;"> </span>
<br></br>
LastName:
<input type="text" name="lastname" id="lastname"
onblur="validateNames(this)"></input>
<span id="lastname_help" style="font-style:italic; color:Red;"> </span>
<center>
<input type="button" value="SUBMIT" onclick="return validateForm(this.form);"></input>
</center>
</form>
<script type="text/javascript">
function validateForm(form) {
document.writeln(validateNames(form["firstname"]));
document.writeln(validateHallticket(form["hallticket"])); // getting undefined value
if (validateNames(form["firstname"]) && validateHallticket(form["hallticket"])) {
form.submit();
}
else {
alert("Please Fill the required Fields");
}
return false;
}
function validateNames(inputField) {
Names_help = document.getElementById('lastname_help');
if (inputField.value.length == 0) {
if (Names_help!= null) {
Names_help.innerHTML = "Please Enter a validate Name";
return false;
}
}
else {
Names_help.innerHTML = "";
return true;
}
return false;
}
function validateHallticket(inputField) {
var regex = /^\d{2}K91A\d{4}$/;
var rs = regex.test(inputField.value);
hallticket_help = document.getElementById('hallticket_help');
if (!regex.test(inputField.value)) {
if (hallticket_help != null) {
hallticket_help.innerHTML = "Enter a valid hallticket";
return false;
}
}
else {
hallticket_help.innerHTML = "";
return true;
}
return false;
}
</script>
</body>
</html>
Upvotes: 1
Reputation: 1121
Put your script tag before ending body tag because you currently dont hav the reference of the element
<body>
<script>
your code in here
</script>
</body>
Upvotes: 0