how to take either String or numbers..in txtbox

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

    how to take either String or numbers..in txtbox

    hi,
    in my project...in one of the textbox...i need to take either numbersnumbers or NULL..im not getting any idea..can anyone help me out..here is my jsp code and js code..

    login.jsp

    Code:
     <td><font color="#800000" size=""><b>L-Acct. No.</b></font></td>
                            <td><input type="text" name="localAccNo" value=NULL></td>
    login.js

    Code:
    if (document.frm.localAccNo.value == "")
        {
            alert("Enter Local A/c No. or 'NULL' in capitals");
            document.frm.localAccNo.focus();
            return (false);
        }
        var chkLocalAccNo = "0123456789";
        var chkLocalAccNoStr = document.frm.localAccNo.value;
        var allchkLocalAccNoValid = true;
        for (i = 0;  i < chkLocalAccNoStr.length;  i++)
        {
            ch = chkLocalAccNoStr.charAt(i);
            for (j = 0;  j < chkLocalAccNo.length;  j++)
                if (ch == chkLocalAccNo.charAt(j))
                    break;
            if (j == chkLocalAccNo.length)
            {
                allchkLocalAccNoValid = false;
                break;
            }
        }
        if (!allchkLocalAccNoValid)
        {
            alert("Enter only numeric in the \" Local A/c No. \" field");
            document.frm.localAccNo.focus();
            return (false);
        }
    thanks,
    madhu.
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    I don’t understand your checking logic. you can test, whether an input qualifies for a number with either isFinite() or isNaN() (parseInt() would be a good starting point either, even RegExp were possible) and how to test for "NULL" I hopefully don’t have to explain.
    Last edited by Dormilich; Jul 16 '10, 09:40 AM.

    Comment

    • madhuriks
      New Member
      • Jun 2010
      • 149

      #3
      hi,
      i checked with the values..if i had given 'NULL' it is taking and if i give 'NULL123' is also taking...but i need is it should take either numbers or NULL that means

      L-Acct. No.----> NULL
      or
      L-Acct. No.----> 123


      like this i need i used RegExp..eventho ugh im not getting can u suggest me how to do..

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        how familiar are you with RegExp?

        Comment

        • madhuriks
          New Member
          • Jun 2010
          • 149

          #5
          i m not that much familiar..im a learner

          i used like this

          Code:
          if (document.frm.localAccNo.value == "")
              {
                  alert("Enter Local A/c No. or 'NULL' in capitals");
                  document.frm.localAccNo.focus();
                  return (false);
              }
              var chkLocalAccNo = "[NULL/0123456] ";
              var chkLocalAccNoStr = document.frm.localAccNo.value;
              var allchkLocalAccNoValid = true;
              for (i = 0;  i < chkLocalAccNoStr.length;  i++)
              {
                  ch = chkLocalAccNoStr.charAt(i);
                  for (j = 0;  j < chkLocalAccNo.length;  j++)
                      if (ch == chkLocalAccNo.charAt(j))
                          break;
                  if (j == chkLocalAccNo.length)
                  {
                      allchkLocalAccNoValid = false;
                      break;
                  }
              }
              if (!allchkLocalAccNoValid)
              {
                  alert("Enter only numeric in the \" Local A/c No. \" field");
                  document.frm.localAccNo.focus();
                  return (false);
              }

          Comment

          • Dormilich
            Recognized Expert Expert
            • Aug 2008
            • 8694

            #6
            no wonder that NULL123 worked …

            ok, part one: how would you (in the simplest way) test for the string "NULL"?

            Comment

            • madhuriks
              New Member
              • Jun 2010
              • 149

              #7
              Code:
              if(document.frm.localAccNo.value == null){
              alert(' null');
              }else if(document.frm.localAccNo.value == 'null'){
              alert('value is a string null');
              }

              Comment

              • Dormilich
                Recognized Expert Expert
                • Aug 2008
                • 8694

                #8
                looks good, but
                - line 1/2 would also fire for "" (empty string).
                - line 3/4 would not fire for "NULL" (or any version with capital letters)

                tip: save the field’s value in a variable, that’s saving you (and the script) a massive amount of time.

                Comment

                • madhuriks
                  New Member
                  • Jun 2010
                  • 149

                  #9
                  i hope isNan() function should be used for this

                  Comment

                  • Dormilich
                    Recognized Expert Expert
                    • Aug 2008
                    • 8694

                    #10
                    isNan() would throw an error (because the function is called isNaN()), but it depends for what you need isNaN(). (as I already mentioned, isNaN() is one way to test a part of your requirements)

                    Comment

                    • madhuriks
                      New Member
                      • Jun 2010
                      • 149

                      #11
                      i didnt got ur point...can u help me how?

                      Comment

                      • Dormilich
                        Recognized Expert Expert
                        • Aug 2008
                        • 8694

                        #12
                        what will you use isNaN() for? there are codes, where isNaN() would definitely help, and codes where it were absolutely pointless.

                        Comment

                        • madhuriks
                          New Member
                          • Jun 2010
                          • 149

                          #13
                          hi,
                          gud mrng..i tried..its getting but the problem is if i enter 'NULL123' it is not showing error..can u suggest me where to change the code..here is my code..

                          login.js

                          Code:
                           var chkLocalAccNo = document.frm.localAccNo.value;
                              //check if it is not a number
                              var validation = true;
                              for (var i = 0; i < chkLocalAccNo.length; i++ )
                              {
                                  if (isNaN(chkLocalAccNo.charAt(i)))
                                  {
                                      //if it is not a number check if it is NULL
                                      if (chkLocalAccNo == 'NULL')
                                      {
                                          validation = true;
                                          document.frm.localAccNo.focus();
                                      }
                                      else
                                      {
                                          validation = false;
                                          document.frm.localAccNo.focus();
                                      }
                                  }
                              }
                          thanx and waiting for the reply.
                          madhu.

                          Comment

                          • Dormilich
                            Recognized Expert Expert
                            • Aug 2008
                            • 8694

                            #14
                            discard the loop.

                            Comment

                            • madhuriks
                              New Member
                              • Jun 2010
                              • 149

                              #15
                              i discard for loop eventhough it is not getting..

                              Comment

                              Working...