0

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

enter image description here

Browser doesn't show an empty string error.

1
  • 2
    Where is it not working? Is it alerting but not stopping the form from submitting? Is it not alerting at all? Is it submitting the form no matter what? Try debugging in some way - placing alerts throughout the Javascript more to track the progress, or something. Look at your browser's console as well, to see any errors the Javascript encounters. Commented Dec 3, 2012 at 16:55

3 Answers 3

1

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

Sign up to request clarification or add additional context in comments.

3 Comments

May I add, fname is not even being used further in the code.
yep, looks like something not removed from the previous used form, at my guess
Thanks for your help. I didn't notice it until you told me.
0

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.

1 Comment

The browser does not pop up with an error when I have empty strings.
0

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);
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.