Reputation: 43
Alright, so I feel my issue is that of a really simple one. basically, I'v created a website, and am trying to use javascript to validate input, all of which initially was one. When input fields were left blank, an error would prompt and so forth. I haven't changed a single thing, and now no form of validation takes place, and it simply ignores my javascript. Any ideas?
Here's an example of the Javascript;
function validate(el){
var alphabets="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ ";
var temp;
if (el.Full_Name.value == "") {
alert("Cannot leave area field blank!");
return false;
}
}
for (var i=0;i<el.Full_Name.value.length;i++){
temp=el.Full_Name.value.substring(i,i+1)
if (alphabets.indexOf(temp)==-1){
alert("Sorry, your name contains a " + temp +", which is not allowed.")
return false
}
}
Here's the HTML:
<form name="testform" onSubmit="return validate(testform)">
<table width="650" border="0">
<tr>
<td width="79" valign="top">
<label for="Full_Name">Full Name<span class="required_star"> * </span></label>
</td>
<td width="289" valign="top">
<input size="15" type="text" name="Full_Name" id="Full_Name" maxlength="50" value="" />
</td>
</tr>
<tr>
<td colspan="4" style="text-align:center">
<input type="submit" value="Proceed"/>
</td>
</tr></table>
</form>
Upvotes: 0
Views: 819
Reputation: 11328
You can speed up/simplify things, a bit:
<form name="testform" onSubmit="return validate(this)">
Notice use of 'this' - it actually refers to form element itself...
JS:
function validate(el) {
if (el.Full_Name.value == "") {
alert("Cannot leave area field blank!");
return false;
}
}
Demo: http://jsfiddle.net/tv9hntkL/
P.S. Correct selector in your case, would be: (document.testform.Full_Name.value == "")
Upvotes: 3
Reputation: 5343
You are accessing the form input wrong.
var myForm = document.forms.<form-name>;
myForm.<input-name>.value; // holds the value of a certain input
will give you access to the value correctly.
Upvotes: 1