tags:

views:

165

answers:

2

Here's my code:

function validate_form(thisform)
{
with (thisform)
  {
      if (validate_required(name,"Name must be filled out!")==false)
  {name.focus();return false;}
  if (validate_required(country," Country must be filled out!")==false)
  {country.focus();return false;}
  if (validate_required(state,"State must be filled out!")==false)
  {state.focus();return false;}
  if (validate_required(city,"City must be filled out!")==false)
  {city.focus();return false;}
  if (validate_required(contact,"Contact must be filled out!")==false)
  {contact.focus();return false;}
  if (validate_required(emailid,"Email must be filled out!")==false)
  {emailid.focus();return false;}
  if (validate_email(userid,"Email is not valid")==false)
  {userid.focus();return false;}
  if (validate_required(password,"pasword must be filed out")==false)
  {password.focus();return false;}
  if (validate_required(cpassword,"Password must be confirmed")==false)
  {cpassword.focus();return false;}

if(validate_required((password.value != cpassword.value),"Your password and confirmation password do not match.")==false) {
cpassword.focus();return false;


}

All other validations are working but not the last one. Why is that so and how to fix it?

+3  A: 

The validate_required function seems to expect an HTML form control (e.g, text input field) as first argument, and check whether there is a value there at all. That is not what you want in this case.

Also, when you write ['password'].value, you create a new array of length one, containing the string 'password', and then read the non-existing property "value" from it, yielding the undefined value.

What you may want to try instead is:

if (password.value != cpassword.value) { cpassword.focus(); return false; }

(You also need to write the error message somehow, but I can't see from your code how that is done.).

Lasse Reichstein
still its not working
bhavna raghuvanshi
the error msg is given as argument to the function
bhavna raghuvanshi
@bhavna raghuvanshi: Are the variables `password` and `cpassword` set so that they contain the DOM elements? For example, what does `alert(password.value)` inside your function show?
Boldewyn
yes they are set
bhavna raghuvanshi
You can't use the validate_required function as written, since it expects an input element and checks if it's empty. I.e., you shouldn't be calling it for the last case at all.You should look at the definition of validate_required and see how it reports the error message and then do the same here when the two password fields' values aren't the same.
Lasse Reichstein
I have checked everything.all other validations are working and displaying the error msg passed as argument.
bhavna raghuvanshi
+1  A: 

I presume you've got validate_required() function from this page: http://www.w3schools.com/js/js_form_validation.asp?

function validate_required(field,alerttxt)
{
with (field)
  {
  if (value==null||value=="")
    {
    alert(alerttxt);return false;
    }
  else
    {
    return true;
    }
  }
}

In this case your last condition will not work as you expect it.

You can replace it with this:

if (password.value != cpassword.value) { 
   alert("Your password and confirmation password do not match.");
   cpassword.focus();
   return false; 
}
DmitryK
I replaced it.But still it doesn't work
bhavna raghuvanshi
in front of "if (password.value != cpassword.value) {" add this: alert(password.value != cpassword.value); what do you get there?
DmitryK
I dont get anything by adding this
bhavna raghuvanshi
You are not getting an alert popup at all???Try just alert(password.value); alert(cpassword.value);Do you get values from both fields?
DmitryK
could you post more of your original/changed code. It is hard to guess what is potentially a typo and what could be a real problem
Java Drinker