onchange() is not working in IE?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • paulrajj
    New Member
    • Sep 2008
    • 47

    onchange() is not working in IE?

    hi to all,

    i am trying to check the validation code using onchange function in internet explorer. but it does not working.. in fire fox its working properly.. please any one give me a solution.

    here is my code,

    <script language="javas cript">
    [CODE=javascript] function val_name(txtCon trol,MsgControl ,strName)
    {

    var txtCtrl=documen t.getElementByI d(txtControl).v alue
    if((txtCtrl.len gth<4) || (txtCtrl.length >20))
    {


    document.getEle mentById(MsgCon trol).innerHTML ="";
    document.getEle mentById(MsgCon trol).innerHTML =strName +" should be between 4 to 20 characters";
    return false;
    }
    else
    {
    document.getEle mentById(MsgCon trol).innerHTML ="";
    }
    document.getEle mentById(txtCon trol).focus();
    return false;

    }

    function name_val(t,v)
    {

    var txtCtrl=documen t.getElementByI d(t);
    var v = "abcdefghijklmn opqrstuvwxyzABC DEFGHIJKLMNOPQR STUVWXYZ ";
    var w = "";
    //alert("welcome" )
    for (i=0; i < txtCtrl.value.l ength; i++)
    {
    x = txtCtrl.value.c harAt(i);
    if (v.indexOf(x,0) != -1)
    w += x;
    }
    txtCtrl.value = w;
    }

    [/CODE] </script>

    [HTML]<input type="text" name="t1" id="t1" onchange="retur n val_name('t1',' fname','sdfdsf' );" onkeyup="name_v al('t1','t1');" />
    <span id="fname" style="color:re d; font-size:11px; "></span>
    <input type="text" name="t2" id="t2" />[/HTML]
    Last edited by acoder; Sep 8 '08, 12:24 PM. Reason: Added [code] tags
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    The onchange doesn't fire in IE. This is caused by the function called onkeyup. I think you're trying to ensure that only alpha characters are entered, but then you set the field value. Rather than that just check the key entered and reject if not an alpha character.

    Comment

    • rnd me
      Recognized Expert Contributor
      • Jun 2007
      • 427

      #3
      don't intercept it by changing the return, there are inconsistancies .

      instead act upon the control and change the input's value in your onchange event.

      onchange fires in IE when the input blurs.

      Comment

      • acoder
        Recognized Expert MVP
        • Nov 2006
        • 16032

        #4
        In this particular case, removing the returns won't make a difference. It's the name_val function which is the cause of the problem because it sets the value of the text box probably erroneously overriding the onchange event.

        Comment

        • paulrajj
          New Member
          • Sep 2008
          • 47

          #5
          Originally posted by acoder
          The onchange doesn't fire in IE. This is caused by the function called onkeyup. I think you're trying to ensure that only alpha characters are entered, but then you set the field value. Rather than that just check the key entered and reject if not an alpha character.
          hi acoder,

          thanks for the answer. i have to check both the onchange and onkeyup functions for my validation purpose. instead of onchange im using onblur function in both browsers(IE & Firefox) its properly executed. if i want to change the onchange instead of onblur means which event i have to use for alpha character check?

          Comment

          • acoder
            Recognized Expert MVP
            • Nov 2006
            • 16032

            #6
            Use a regular expression to test the character entered. If it's not valid, return false. Note that this won't work with onkeyup, but you can use onkeypress/down instead. See this link for a number example. Change it to test for alpha characters instead.

            Comment

            • paulrajj
              New Member
              • Sep 2008
              • 47

              #7
              Originally posted by acoder
              Use a regular expression to test the character entered. If it's not valid, return false. Note that this won't work with onkeyup, but you can use onkeypress/down instead. See this link for a number example. Change it to test for alpha characters instead.

              hi acoder,

              i tried with that example. but in that example input values allow special characters. if i need to check only for alpha characters not for numbers and special characters means is there any regular expression available to check for only alpha characters?

              Comment

              • acoder
                Recognized Expert MVP
                • Nov 2006
                • 16032

                #8
                Yes, /[a-z]/i would check for alpha characters only.

                Comment

                • paulrajj
                  New Member
                  • Sep 2008
                  • 47

                  #9
                  Originally posted by acoder
                  Yes, /[a-z]/i would check for alpha characters only.
                  thanks for ur answer acoder.

                  is it possible to call onchange and onblur events inside the same tag? while im doing these events are not worked properly?

                  Comment

                  • acoder
                    Recognized Expert MVP
                    • Nov 2006
                    • 16032

                    #10
                    It is, but it depends on what you're trying to do. Are you calling the same function for both?

                    Comment

                    • paulrajj
                      New Member
                      • Sep 2008
                      • 47

                      #11
                      Originally posted by acoder
                      It is, but it depends on what you're trying to do. Are you calling the same function for both?
                      i am calling different functions inside the same tag using onchange and onblur.. pls give me an example.

                      Comment

                      • acoder
                        Recognized Expert MVP
                        • Nov 2006
                        • 16032

                        #12
                        Show the functions you're calling. It might be that one is affecting the other.

                        Comment

                        • paulrajj
                          New Member
                          • Sep 2008
                          • 47

                          #13
                          Originally posted by acoder
                          Show the functions you're calling. It might be that one is affecting the other.
                          i got the answer by giving to diifrent tags. but when im including the events inside the same tag im getting some problem with validation error message.
                          my code here..

                          <script type="text/javascript">

                          [CODE=javascript]
                          function apply(ddl,txt)
                          {
                          var getValue=ddl.op tions[ddl.selectedInd ex].text;
                          if(getValue=='O thers')
                          {
                          document.getEle mentById(txt).d isabled=false;
                          }
                          else
                          {
                          document.getEle mentById(txt).v alue="";
                          document.getEle mentById(txt).d isabled=true;
                          }
                          }
                          function defect_check(tx tControl,ddlCon trol,spanid)
                          {

                          var ctrlvalue=docum ent.getElementB yId(txtControl) .value;
                          var ddlctrl=documen t.getElementByI d(ddlControl).v alue;
                          if((ddlctrl=="O thers")&&(ctrlv alue.length==0) )
                          {
                          document.getEle mentById(spanid ).innerHTML="Pl ease enter the data here.";
                          document.getEle mentById(txtCon trol).focus;
                          //return false;
                          }
                          else
                          {
                          document.getEle mentById(spanid ).innerHTML="";
                          }

                          }
                          [/CODE]
                          </script>
                          [HTML]<form name="">
                          <select name="rehab" id="rehab" style="width:15 0px;" onchange="apply (this,'rehab_ot her');" onblur="return defect_check('r ehab_other','re hab','dob'); ">
                          <option value="Select" selected="selec ted">Select</option>
                          <option value="One">One </option>
                          <option value="Two">Two </option>
                          <option value="Three">T hree</option>
                          <option value="Others"> Others</option>
                          </select>
                          </strong>&nbsp;
                          <input name="rehab_oth er" id="rehab_other " type="text" class="" value="" size="20" maxlength="40" disabled="disab led" />&nbsp;<span id="dob" style="color:re d; font-size:11px; "></span>
                          </form>[/HTML]
                          Last edited by acoder; Sep 10 '08, 12:14 PM. Reason: Added [code] tags

                          Comment

                          • acoder
                            Recognized Expert MVP
                            • Nov 2006
                            • 16032

                            #14
                            You mean the "Please enter the data here" message?

                            PS. please use code tags when posting code. See How to ask a question. Thanks!

                            Comment

                            • paulrajj
                              New Member
                              • Sep 2008
                              • 47

                              #15
                              Originally posted by acoder
                              You mean the "Please enter the data here" message?

                              PS. please use code tags when posting code. See How to ask a question. Thanks!
                              the message is for validation.. i want to check for both validation and enable the field..

                              thanks for giving me the link...

                              Comment

                              Working...