Jump to content

Recommended Posts

I am trying to create a function where it checks to see if the form variable companyfield has a value of "".  If it does, it should show an error.  Otherwise, it checks to see if companyfield has a value of Other.  If it does, it then needs to check the formfield companyfieldother.  If that has a value of "", it shows an error.  Otherwise, it returns true.  I'll send a copy of a password checking function (ConfirmRegPass) that goes with the script to see how I'm supposed to form it.  Please help  :confused:

 

 

Remember, ConfirmRegPass works flawlessly with what I'm trying to accomplish.

 

function ConfirmRegPass()
{
  var frm = document.forms["register"];
  if(frm.password.value != frm.password2.value)
  {
    sfm_show_error_msg("The Password and verified password don't match!",frm.password1);
    return false;
  }
  else
  {
    return true;
  }
}

function Companyfield()
{
  var frm = document.forms["register"];
  if(frm.companyfield.value == "")
  {
    sfm_show_error_msg("Please select a company field.",frm.companyfield);
    return false;
  }
  else
  {
  if(frm.companyfield.value == "Other")
  {
	  if(frm.companyfieldother.value == ""){
	  sfm_show_error_msg("Please type your company field.",frm.companyfieldother);
	  return false;
	  }
	  else{
	  return true;
	  }
  }
  else
  {	  
      return true;
  }
  }
}

Link to comment
https://forums.phpfreaks.com/topic/204314-help-with-a-function/
Share on other sites

It's difficult to debug your code without seeing all the applicable code. Could be you are using the wrong field names or something. But, your code has more "logic" than it needs. For example you check passowrd function does not need an else. The IF will exit the function of validation fails, so you simply need to return true after the IF code.

 

Assuming the code is referncing the correct fields and the values are what you expect, the code looks like it should work, but here is a rewrite that is more efficient and logical, IMO.

 

function ConfirmRegPass()
{
  var frm = document.forms["register"];
  if(frm.password.value != frm.password2.value)
  {
    sfm_show_error_msg("The Password and verified password don't match!", frm.password1);
    return false;
  }
  return true;
}

function Companyfield()
{
  var frm = document.forms["register"];
  if(frm.companyfield.value == "")
  {
    sfm_show_error_msg("Please select a company field.",frm.companyfield);
    return false;
  }
  else if(frm.companyfield.value=="Other" && frm.companyfieldother.value == "")
  {
    sfm_show_error_msg("Please type your company field.", frm.companyfieldother);
    return false;
  }
  return true;
}

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.