views:

32

answers:

1

I have some simple form validation going on, and it works in FF and IE. Simply put, if there is a date out of range on the form, javascript prevents the form submission. However, in Chrome, it's totally broken. When the preventDefault fires, Chrome logs the user out of the application and they get kicked back to the index/login page. And I have no clue why.

The code I am using is here:

function cancelEvent (e) {
  if (e.preventDefault) {
    e.preventDefault();
  } else {
    e.returnValue = false;
  }
} 

It's fed by

function validateFields(evt) {
  evt = evt ? evt : window.event;

  var invalid = false;

  //validation #1: Consent Date should NOT be in the future  
  var today = new Date();
  var consentDate = document.forms["ptform"].elements["calConsent"].value;
  var consDate=convertDate(consentDate);

  if(consDate>today){
    invalid=true;
    var consentDateTitle=document.getElementById("consentDateTitle");
    consentDateTitle.className = consentDateTitle.className + " errormessage";
    alert("Consent Date cannot be in the future");
  }
  //end validation #1

  if (invalid) {
    cancelEvent(evt);
  }
}

And that's pretty much it. Works fine in FF and IE but Chrome freaks out. Any ideas why? I'm not a js expert... :(

my event handler code:

// listen to an event
function listenEvent(eventObj, event, eventHandler) {
  if (eventObj.addEventListener) {
    eventObj.addEventListener(event, eventHandler,false);
  } else if (eventObj.attachEvent) {
    event = "on" + event;
    eventObj.attachEvent(event, eventHandler);
  } else {
    eventObj["on" + event] = eventHandler;
  }
}
A: 

Can I spiffy up your code a bit?

function validateFields(evt) {
  evt = evt || window.event;

  var invalid; //Null qualifies as false too

  //validation #1: Consent Date should NOT be in the future  
  var today = new Date();
  var consentDate = document.forms["ptform"].elements["calConsent"].value;
  var consDate=convertDate(consentDate);

  if(consDate>today){
    invalid=true;
    var consentDateTitle=document.getElementById("consentDateTitle");
    consentDateTitle.className += " errormessage";
    alert("Consent Date cannot be in the future");
    return false;  //Prevents bubbling and default behavior
  }
  //end validation #1

}
Drew
Hm. Maybe i'm implementing that wrong, but that works in IE, allows in improper submission in ff, and the alert still throws the user out in chrome. :(
cephyn
It's something to do with the alert. any triggered alert kicks the user out. the validation code i have works - it just breaks on the alert box. im kinda baffled, but ill have to do more debugging....
cephyn
I had a feeling the alert might be what is acting strange. Remember alert halts all JS execution. It might be worth it to create a little popup box, that way you can pass back messages to the user without halting your application.
Drew