Reputation: 8400
It has to be a minor issue but I just can't find it. Maybe one of you can help me here.
Javascript
function white_space(field)
{
field.value = field.value.replace(/^\s+/, "");
}
function validate(){
var formname=document.reg_form;
var fname=formname.fname.value;
var mess="";
var uname=formname.prodName.value;
if(uname==""){
mess= mess+"User Name cannot be blank\n";
}
var pass=formname.prodPrice.value;
if(pass==""){
mess= mess+"Password cannot be blank\n";
}
if(mess){
alert(mess);
return false;
}
}
HTML FORM
<form class="form-signin" action="#" onSubmit="return validate();" name="reg_form">
<h2 class="form-signin-heading">Sign-In</h2>
<input type="text" class="input-block-level" name="prodName" placeholder="Email address" onKeyPress="white_space(this)">
<input type="password" class="input-block-level" name="prodPrice" placeholder="Password" onKeyPress="white_space(this)">
<label class="checkbox">
<input type="checkbox" value="remember-me"> Remember me
</label>
<button class="btn btn-large btn-primary" type="submit">Sign in</button>
</form>
Error
Browser doesn't show an empty string error.
Upvotes: 0
Views: 67
Reputation: 3532
in your case this line
var fname=formname.fname.value;
throw error while execution validate function, as there is no element with fname
into the form, comment it and you are fine
http://jsbin.com/oxawil/1
Upvotes: 1
Reputation: 367
You could use jQuery for easier selecting parts of your html. Check out this jsFiddle for a working solution.
Please mind it is not safe checking the values in javascript and assuming the values are correct in your security level. So its more common to submit the form and return if the input is valide.
$.ajax('url', function(json) {
var msg = '';
switch(json.status) {
case 'name':
msg += 'name not set';
break;
case 'pwd':
msg += 'password not set';
break;
}
if(msg != '') {
alert(msg);
}
}
Upvotes: 0
Reputation: 484
Not knowing what the behaviour is that's undesirable (you didn't say), I'm guessing it has to do with the lack of a positive return value if the validate function succeeds. The code just falls off the end.
Upvotes: 0