IE vs Firefox Javascript Validation Problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • cbs7
    New Member
    • Apr 2007
    • 4

    IE vs Firefox Javascript Validation Problem

    Hi all

    I'm a complete newbie to web development and Javascript especially. I've been creating a form for a webpage and have used a validation script gen_validatorv2 .js which I downloaded from the zip file referenced on http://www.javascript-coder.com/html...lidation.phtml

    I managed to get everything working and was testing through Firefox that alerts were generated. However at the very end I thought I'd better check it was all ok on IE and found that the alerts aren't being generated at all.

    The scenario is that I have a form with several fields to complete and I tested it by leaving some of the mandatory ones blank. When I click on submit, the alerts pop up in Firefox and prevent the form being submitted. In IE, I don't get any alerts popping up and the form doesn't submit either.

    As I'm completely new, I really don't know where to start on this and my web searches haven't yet come up with anything. I am happy to post code to this thread, but am not sure where to start. If you experts are able to advise, I would be very grateful :) Are there any well known differences between how IE and Firefox handle Javascript?

    Many thanks
    cbs7
  • Ansuiya
    New Member
    • Jan 2007
    • 40

    #2
    hi can u please post the code.

    Comment

    • cbs7
      New Member
      • Apr 2007
      • 4

      #3
      Thanks for the reply :)

      The webpage is quite long so I'll try and post the relevant bits

      1. This is within the <head> </head> section

      <script src="gen_valida torv2.js" type="text/javascript"></script>

      2. To start the form I have

      <form name="myform" method="post" action="Contact _Us_with_java_p 2.php" />

      3. This performs the validation and comes just after the end of </form>

      <script type="text/javascript">
      var frmvalidator = new Validator("myfo rm");
      frmvalidator.ad dValidation("Re asonForSale","r eq","Please select your Reason For Sale");
      frmvalidator.ad dValidation("Wo uldLikeToSellAn dRentBack","req ","Please select if you would like to Sell and Rent Back");
      frmvalidator.ad dValidation("Wa ntToSellWithin" ,"req","Plea se select How Quickly You Need to Sell");
      frmvalidator.ad dValidation("Fi rstName","req", "Please enter your First Name");
      frmvalidator.ad dValidation("La stName","req"," Please enter your Last Name");
      frmvalidator.ad dValidation("Em ail","email","P lease enter a valid email address");
      frmvalidator.se tAddnlValidatio nFunction("Chec kContactNumber" );
      </script>

      4. Below is the first part of the gen_validatorv2 .js code ( I need to post the 2nd part in another post as it is too long)

      /*
      -------------------------------------------------------------------------
      JavaScript Form Validator
      Version 2.0.2
      Copyright 2003 JavaScript-coder.com. All rights reserved.
      You use this script in your Web pages, provided these opening credit
      lines are kept intact.
      The Form validation script is distributed free from JavaScript-Coder.com

      You may please add a link to JavaScript-Coder.com,
      making it easy for others to find this script.
      Checkout the Give a link and Get a link page:
      http://www.javascript-coder.com/links/how-to-link.php

      You may not reprint or redistribute this code without permission from
      JavaScript-Coder.com.

      JavaScript Coder
      It precisely codes what you imagine!
      Grab your copy here:
      http://www.javascript-coder.com/
      -------------------------------------------------------------------------
      */
      function Validator(frmna me)
      {
      this.formobj=do cument.forms[frmname];
      if(!this.formob j)
      {
      alert("BUG: couldnot get Form object "+frmname);
      return;
      }
      if(this.formobj .onsubmit)
      {
      this.formobj.ol d_onsubmit = this.formobj.on submit;
      this.formobj.on submit=null;
      }
      else
      {
      this.formobj.ol d_onsubmit = null;
      }
      this.formobj.on submit=form_sub mit_handler;
      this.addValidat ion = add_validation;
      this.setAddnlVa lidationFunctio n=set_addnl_vfu nction;
      this.clearAllVa lidations = clear_all_valid ations;
      }
      function set_addnl_vfunc tion(functionna me)
      {
      this.formobj.ad dnlvalidation = functionname;
      }
      function CheckContactNum ber()
      {
      var frm = document.forms["myform"];
      if (frm.ContactNum ber.value != frm.ConfirmCont actNumber.value )
      {
      alert ( "Contact Numbers Must Be Identical." );
      return false;
      }
      else
      {
      return true;
      }
      }
      function CheckPrivacyPol icyAgreement()
      {
      var frm = document.forms["myform"];
      if (frm.PrivacyPol icyAgreement.ch ecked == false)
      {
      alert ( "Please tick the box to confirm you have read and agreed to our Privacy Policy." );
      return false;
      }
      else
      {
      return true;
      }
      }
      function clear_all_valid ations()
      {
      for(var itr=0;itr < this.formobj.el ements.length;i tr++)
      {
      this.formobj.el ements[itr].validationset = null;
      }
      }
      function form_submit_han dler()
      {
      for(var itr=0;itr < this.elements.l ength;itr++)
      {
      if(this.element s[itr].validationset &&
      !this.elements[itr].validationset. validate())
      {
      return false;
      }
      }
      if(this.addnlva lidation)
      {
      str =" var ret = "+this.addnlval idation+"()";
      eval(str);
      if(!ret) return ret;
      }
      return true;
      }

      Comment

      • cbs7
        New Member
        • Apr 2007
        • 4

        #4
        2nd Part of gen_validatorv2 .js code
        --------------------------------------------------

        function add_validation( itemname,descri ptor,errstr)
        {
        if(!this.formob j)
        {
        alert("BUG: the form object is not set properly");
        return;
        }//if
        var itemobj = this.formobj[itemname];
        if(!itemobj)
        {
        alert("BUG: Couldnot get the input object named: "+itemname) ;
        return;
        }
        if(!itemobj.val idationset)
        {
        itemobj.validat ionset = new ValidationSet(i temobj);
        }
        itemobj.validat ionset.add(desc riptor,errstr);
        }
        function ValidationDesc( inputitem,desc, error)
        {
        this.desc=desc;
        this.error=erro r;
        this.itemobj = inputitem;
        this.validate=v desc_validate;
        }
        function vdesc_validate( )
        {
        if(!V2validateD ata(this.desc,t his.itemobj,thi s.error))
        {
        this.itemobj.fo cus();
        return false;
        }
        return true;
        }
        function ValidationSet(i nputitem)
        {
        this.vSet=new Array();
        this.add= add_validationd esc;
        this.validate= vset_validate;
        this.itemobj = inputitem;
        }
        function add_validationd esc(desc,error)
        {
        this.vSet[this.vSet.lengt h]=
        new ValidationDesc( this.itemobj,de sc,error);
        }
        function vset_validate()
        {
        for(var itr=0;itr<this. vSet.length;itr ++)
        {
        if(!this.vSet[itr].validate())
        {
        return false;
        }
        }
        return true;
        }
        function validateEmailv2 (email)
        {
        // a very simple email validation checking.
        // you can add more complex email checking if it helps
        if(email.length <= 0)
        {
        return true;
        }
        var splitted = email.match("^( .+)@(.+)$");
        if(splitted == null) return false;
        if(splitted[1] != null )
        {
        var regexp_user=/^\"?[\w-_\.]*\"?$/;
        if(splitted[1].match(regexp_u ser) == null) return false;
        }
        if(splitted[2] != null)
        {
        var regexp_domain=/^[\w-\.]*\.[A-Za-z]{2,4}$/;
        if(splitted[2].match(regexp_d omain) == null)
        {
        var regexp_ip =/^\[\d{1,3}\.\d{1,3 }\.\d{1,3}\.\d{ 1,3}\]$/;
        if(splitted[2].match(regexp_i p) == null) return false;
        }// if
        return true;
        }
        return false;
        }
        function V2validateData( strValidateStr, objValue,strErr or)
        {
        var epos = strValidateStr. search("=");
        var command = "";
        var cmdvalue = "";
        if(epos >= 0)
        {
        command = strValidateStr. substring(0,epo s);
        cmdvalue = strValidateStr. substr(epos+1);
        }
        else
        {
        command = strValidateStr;
        }
        switch(command)
        {
        case "req":
        case "required":
        {
        if(eval(objValu e.value.length) == 0)
        {
        if(!strError || strError.length ==0)
        {
        strError = objValue.name + " : Required Field";
        }//if
        alert(strError) ;
        return false;
        }//if
        break;
        }//case required
        case "maxlength" :
        case "maxlen":
        {
        if(eval(objValu e.value.length) > eval(cmdvalue))
        {
        if(!strError || strError.length ==0)
        {
        strError = objValue.name + " : "+cmdvalue+ " characters maximum ";
        }//if
        alert(strError + "\n[Current length = " + objValue.value. length + " ]");
        return false;
        }//if
        break;
        }//case maxlen
        case "minlength" :
        case "minlen":
        {
        if(eval(objValu e.value.length) < eval(cmdvalue))
        {
        if(!strError || strError.length ==0)
        {
        strError = objValue.name + " : " + cmdvalue + " characters minimum ";
        }//if
        alert(strError + "\n[Current length = " + objValue.value. length + " ]");
        return false;
        }//if
        break;
        }//case minlen
        case "alnum":
        case "alphanumer ic":
        {
        var charpos = objValue.value. search("[^A-Za-z0-9]");
        if(objValue.val ue.length > 0 && charpos >= 0)
        {
        if(!strError || strError.length ==0)
        {
        strError = objValue.name+" : Only alpha-numeric characters allowed ";
        }//if
        alert(strError + "\n [Error character position " + eval(charpos+1) +"]");
        return false;
        }//if
        break;
        }//case alphanumeric
        case "num":
        case "numeric":
        {
        var charpos = objValue.value. search("[^0-9]");
        if(objValue.val ue.length > 0 && charpos >= 0)
        {
        if(!strError || strError.length ==0)
        {
        strError = objValue.name+" : Only digits allowed ";
        }//if
        alert(strError + "\n [Error character position " + eval(charpos+1) +"]");
        return false;
        }//if
        break;
        }//numeric
        case "alphabetic ":
        case "alpha":
        {
        var charpos = objValue.value. search("[^A-Za-z]");
        if(objValue.val ue.length > 0 && charpos >= 0)
        {
        if(!strError || strError.length ==0)
        {
        strError = objValue.name+" : Only alphabetic characters allowed ";
        }//if
        alert(strError + "\n [Error character position " + eval(charpos+1) +"]");
        return false;
        }//if
        break;
        }//alpha
        case "alnumhyphe n":
        {
        var charpos = objValue.value. search("[^A-Za-z0-9\-_]");
        if(objValue.val ue.length > 0 && charpos >= 0)
        {
        if(!strError || strError.length ==0)
        {
        strError = objValue.name+" : characters allowed are A-Z,a-z,0-9,- and _";
        }//if
        alert(strError + "\n [Error character position " + eval(charpos+1) +"]");
        return false;
        }//if
        break;
        }
        case "email":
        {
        if(!validateEma ilv2(objValue.v alue))
        {
        if(!strError || strError.length ==0)
        {
        strError = objValue.name+" : Enter a valid Email address ";
        }//if
        alert(strError) ;
        return false;
        }//if
        break;
        }//case email
        case "lt":
        case "lessthan":
        {
        if(isNaN(objVal ue.value))
        {
        alert(objValue. name+": Should be a number ");
        return false;
        }//if
        if(eval(objValu e.value) >= eval(cmdvalue))
        {
        if(!strError || strError.length ==0)
        {
        strError = objValue.name + " : value should be less than "+ cmdvalue;
        }//if
        alert(strError) ;
        return false;
        }//if
        break;
        }//case lessthan
        case "gt":
        case "greatertha n":
        {
        if(isNaN(objVal ue.value))
        {
        alert(objValue. name+": Should be a number ");
        return false;
        }//if
        if(eval(objValu e.value) <= eval(cmdvalue))
        {
        if(!strError || strError.length ==0)
        {
        strError = objValue.name + " : value should be greater than "+ cmdvalue;
        }//if
        alert(strError) ;
        return false;
        }//if
        break;
        }//case greaterthan
        case "regexp":
        {
        if(objValue.val ue.length > 0)
        {
        if(!objValue.va lue.match(cmdva lue))
        {
        if(!strError || strError.length ==0)
        {
        strError = objValue.name+" : Invalid characters found ";
        }//if
        alert(strError) ;
        return false;
        }//if
        }
        break;
        }//case regexp
        case "dontselect ":
        {
        if(objValue.sel ectedIndex == null)
        {
        alert("BUG: dontselect command for non-select Item");
        return false;
        }
        if(objValue.sel ectedIndex == eval(cmdvalue))
        {
        if(!strError || strError.length ==0)
        {
        strError = objValue.name+" : Please Select one option ";
        }//if
        alert(strError) ;
        return false;
        }
        break;
        }//case dontselect
        }//switch
        return true;
        }
        /*
        Copyright 2003 JavaScript-coder.com. All rights reserved.
        */

        Comment

        • cbs7
          New Member
          • Apr 2007
          • 4

          #5
          Hi there

          Sorry I found that I had some html syntax causing the problem. Thanks for the help

          cbs7

          Comment

          • Ansuiya
            New Member
            • Jan 2007
            • 40

            #6
            hi i have not seen your code yet but if there is any error in your HTML as u hav already written then u can confirm it from validator about ur error.validator will show the error and line no. where the error occurred.check the html code using validator.
            the site for validator is

            validator.w3.or g

            Comment

            Working...