how to take either single digit or double digit in textbox

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • madhuriks
    New Member
    • Jun 2010
    • 149

    #1

    how to take either single digit or double digit in textbox

    hi gud mrng,
    in my form....i need to take either single digit in textbox i.e., '1' or '12'..what i did is as follows

    login.jsp

    Code:
    <td><font color="#800000" size=""><b>Alert-Msg-Time (in Hrs)</b></font></td>
    <td><input type="text" name="alertMsg" maxlength="6"></td>
    <td></td>
    login.js

    Code:
    if (document.frm.alertMsg.value == "")
      {
           alert("Enter Alert Msg Time");
           document.frm.alertMsg.focus();
           return (false);
       }
       var chkAlrtmsg = /^[0123456789]{2}$/;
       if (chkAlrtmsg.test(document.frm.alertMsg.value))
       {}
       else
       {
           alert('Invalid \" Alert Msg Time \" Entry');
           return false;
       }
    here i used regExp for taking 2 digits...but i need is it should take either of them....can anyone help me out...

    thanks,
    madhu.
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    why did you define a size (textbox) of 6, when you only need one of 2?

    PS.
    Code:
    /^\d{1,2}$/

    Comment

    • madhuriks
      New Member
      • Jun 2010
      • 149

      #3
      hi,
      thanks..its working i removed the 'maxlength'...i had another problem with another label..called 'L-Units'....in this txtbox i need to enter 'decimal numbers' as well as it should not take letters nor -ve values...here is my code.

      login.jsp

      Code:
      <td></td>
        <td><font color="#800000" size=""><b>L-Units</b>&nbsp;&nbsp;</font></td>
        <td><input type="text" name="localUnits" id="localUnits">&nbsp;&nbsp;&nbsp;&nbsp;</td>
      <td></td>
      login.js

      Code:
      if (document.frm.localUnits.value == "")
          {
              alert("Enter Local Units");
              document.frm.localUnits.focus();
              return false;
          }
          var chklocalUnits = "0123456789";
          var checklocalUnitsStr = document.frm.localUnits.value;
          var alllocalUnitsValid = true;
          for (i = 0;  i < checklocalUnitsStr.length;  i++)
          {
              ch = checklocalUnitsStr.charAt(i);
              for (j = 0;  j < chklocalUnits.length;  j++)
                  if (ch == chklocalUnits.charAt(j))
                      break;
              if (j == chklocalUnits.length)
              {
                  alllocalUnitsValid = false;
                  break;
              }
          }
          if (!alllocalUnitsValid)
          {
              alert("Enter only numeric in the \" Local Units \" field , No -ve's are allowed.");
              document.frm.localUnits.focus();
              return (false);
          }
          var localUnitsValid = document.getElementById('localUnits').value;
          for(var i1 = 0; i1 < localUnitsValid.length; i1++)
          {
              currentCode = localUnitsValid.charCodeAt(i1);
              if(currentCode!=45 && currentCode!=46 && currentCode<48 || currentCode>57)
              {
                  alert('Only Numbers are allowed in Local Units Valid');
                  return false;
              } 
              if(localUnitsValid.indexOf('.')!=-1 && localUnitsValid.lastIndexOf('.')!=localUnitsValid.indexOf('.')){
                  alert('A number can only have one decimal point');
                  return false;
              }
          }
      wht i wrote in js it is not accepting 'Decimal numbers'...but it is accepting 'No -ve values to be entered and letters not to be entered'....can u suggest where i wnt wrong...

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        what is a "-ve" value?

        Comment

        • madhuriks
          New Member
          • Jun 2010
          • 149

          #5
          '-ve' means 'negative' value....

          Comment

          • madhuriks
            New Member
            • Jun 2010
            • 149

            #6
            the txtbox should not take 'negative' values...whts wrng in my code...

            Comment

            • Dormilich
              Recognized Expert Expert
              • Aug 2008
              • 8694

              #7
              IMO, too complicated test logic. convert the value into a number (parseFloat()) and test for NaN (not a numerical input) and x < 0.

              Comment

              • madhuriks
                New Member
                • Jun 2010
                • 149

                #8
                i had deleted some part of js code...here the code is

                Code:
                var stdUnitsValid = document.getElementById('stdUnits').value;
                    for(var i2 = 0; i2 < stdUnitsValid.length; i2++)
                    {
                        currentCode = stdUnitsValid.charCodeAt(i2);
                        if(currentCode!=45 && currentCode!=46 && currentCode<48 || currentCode>57)
                        {
                            alert('Only Numbers are allowed in Local Units Valid');
                            return false;
                        }
                        if(stdUnitsValid.indexOf('.')!=-1 && stdUnitsValid.lastIndexOf('.')!=stdUnitsValid.indexOf('.')){
                            alert('A number can only have one decimal point');
                            return false;
                        }
                    }

                Comment

                • Dormilich
                  Recognized Expert Expert
                  • Aug 2008
                  • 8694

                  #9
                  I am still of the opinion, that you think too complicated. convert the input in a number and test, whether it is negative.

                  Comment

                  • madhuriks
                    New Member
                    • Jun 2010
                    • 149

                    #10
                    i tried what u suggested...the problem im facing is if i enter the negative no. it is not showing any alert..i used if condition but not wrkng...hw to check for that..i removed if condition ..my code is

                    Code:
                    if (document.frm.localUnits.value == "")
                        {
                            alert("Enter Local Units");
                            document.frm.localUnits.focus();
                            return false;
                        }
                        
                        var localUnitsValid = document.getElementById('localUnits').value;
                        for(var i2 = 0; i2 < localUnitsValid.length; i2++)
                        {
                            currentCode = localUnitsValid.charCodeAt(i2);
                            if(currentCode!=45 && currentCode!=46 && currentCode<48 || currentCode>57)
                            {
                                alert('Only Numbers are allowed in STD Units Valid');
                                return false;
                            }
                        }
                        localUnitsValid = Math.abs(localUnitsValid);
                        document.getElementById('localUnits').value = Math.abs(localUnitsValid)

                    Comment

                    • Dormilich
                      Recognized Expert Expert
                      • Aug 2008
                      • 8694

                      #11
                      i tried what u suggested...
                      according to your posted code, you did not.

                      Comment

                      • madhuriks
                        New Member
                        • Jun 2010
                        • 149

                        #12
                        sorry for that i used 'Math' function...and i got it...by submitting the form..'negative ' values converts to postive value in my o/p..but i need alert msg...where shld i code..

                        Comment

                        • madhuriks
                          New Member
                          • Jun 2010
                          • 149

                          #13
                          hi,
                          im getting alert msg...i came to knw where i kept wrng..here is my chngd code..
                          Code:
                          if (localUnitsValid != Math.abs(localUnitsValid))
                                      {
                                          alert('entered -ve value');
                                          localUnitsValid = Math.abs(localUnitsValid);
                                          return false;
                                      }

                          Comment

                          • Dormilich
                            Recognized Expert Expert
                            • Aug 2008
                            • 8694

                            #14
                            what about if (localUnitsVali d < 0) ?

                            Comment

                            • madhuriks
                              New Member
                              • Jun 2010
                              • 149

                              #15
                              im entering the values in txtbox...if the txt box is left empty..i'll throw an alert...

                              Comment

                              Working...