Wrox Home  
Search P2P Archive for: Go

  Return to Index  

javascript thread: Saying a field is empty when it isn't


Message #1 by "Greg Dunn" <greg.dunn@n...> on Fri, 10 Jan 2003 17:17:26 -0600
I apologize for the length of this, but I'm not sure where the error is.

Some users report that they're told they need to fill in the Credit Card
field even though they have done so.

Does anyone see anything that might cause this?  Is having different name
and id attribute values for a field a problem?



This is the field:

<INPUT TYPE="text" NAME="x_Card_Num" ID="REQx_Card_Num" SIZE=16
MAXLENGTH=16 VALUE="" onChange="validate_field(this, 13, 16, 'Card #')">




This is the script:


var submitOk = 'n';
var isIE = (navigator.appName.indexOf("Microsoft") != -1);

function checkFld(field) {
  var msg = "";
  if (isIE) {
    msg = eval("document.all." + field.name + "ReqMsg");
  }
  if (!field.value) {
    submitOk = 'n';
    if (isIE) {
      msg.innerText = "- Must be entered";
    }
  }
  else if (isIE) {
    msg.innerText = "";
  }
}

function validate_form(form) {
  if (isIE) {
    for (var i=0; i < form.elements.length; i++) {
      if (form.elements[i].id.substr(0,3) == "REQ") {
        checkFld(eval(form.elements[form.elements[i].id.substr(3)]));
      }
    }
  }
  else {
    checkFld(form.elements['x_Address']);
    checkFld(form.elements['x_Zip']);
    checkFld(form.elements['x_Card_Num']);
    checkFld(form.elements['x_Exp_Date']);
  }
}

function validate_field(field, minLen, maxLen, title) {
	if (field.name == "x_Card_Num" || field.name == "x_Exp_Date") {
		if (!checkNbr(field, 'n', 'n')) {
			submitOk = 'n';
		}
	}
	if (field.value.length < minLen) {
		submitOk = 'n';
		alert(title + ": At least " + minLen + " numbers must be entered into this
box."
				+ "\r\rYou entered " + field.value.length + " numbers.");
	}
	else if (field.value.length > maxLen) {
		submitOk = 'n';
		alert(title + ": No more than " + maxLen + " numbers may be entered into
this box."
				+ "\r\rYou entered " + field.value.length + " numbers.");
	}
}

function submitForm(form) {
  submitOk = 'y';
  validate_field(form.elements['x_Card_Num'], 13, 16);
  validate_field(form.elements['x_Exp_Date'], 4, 4);
  validate_form(form);
  if (submitOk == 'y') {
     form.elements['btnSubmit'].value = "Processing...";
     window.status="Processing credit/debit card transaction, please
wait...";
     form.submit();
     form.reset();
  }
}


Thanks,

Greg


  Return to Index