code to find whether only numerals are entered in a text box

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jayakrishnanav
    New Member
    • Jan 2008
    • 10

    code to find whether only numerals are entered in a text box

    Hi ,


    Can anybody help me with the code , to find whether only numerals are entered in a text box (using js.),at the time of saving.

    jk
  • chaarmann
    Recognized Expert Contributor
    • Nov 2007
    • 785

    #2
    Originally posted by jayakrishnanav
    Hi ,


    Can anybody help me with the code , to find whether only numerals are entered in a text box (using js.),at the time of saving.

    jk
    Just put an onKeyUp-event and onChange-event for the text box which calls a javascript function.
    Or call this function in your onSubmit() function of your form.
    This javascript function, which you can put into your ".js" file, uses a regular expression that checks for digits.
    If you do it this way with regExpr., then you can write the function by using only one line of source code.

    Comment

    • gits
      Recognized Expert Moderator Expert
      • May 2007
      • 5388

      #3
      hi ...

      a regExp would do the job. in case you only want integers you may use something like the following for it:

      [CODE=javascript]// val may be you input-value
      var val = '123';

      // function that checks for numbers - returns true when
      // ok otherwise it returns false (the regExp is literally created
      // with /^\d+$/ and checks for numbers from start to end)
      function test_value(val) {
      return /^\d+$/.test(val);
      }

      // now we could call the function
      alert(test_valu e(val));
      [/CODE]
      kind regards

      Comment

      • jayakrishnanav
        New Member
        • Jan 2008
        • 10

        #4
        hi frnds,

        thanx for ur fast reply! i hv done tat as u said and itz workin!!!

        gitz,actually i want to check whether only integers are entered in the field and if there is notin other than integers,then i hv to reject it!I hv done tat using the code u hv given.
        Then i need a thing similar to this,tat is i want to check whether only special characters are entered in a textfield?
        can u help me in tat?


        thanks in advace
        jk

        Comment

        • gits
          Recognized Expert Moderator Expert
          • May 2007
          • 5388

          #5
          :) glad to hear that ... for further information you may have a look here
          and may be for your next purpose you may use:

          [CODE=javascript]/^\W$/[/CODE]
          instead of

          [CODE=javascript]/^\d$/[/CODE]
          kind regards

          Comment

          • jayakrishnanav
            New Member
            • Jan 2008
            • 10

            #6
            Hi gits,

            Thanks again ...by replacing the function as u said,itz now possible to check the existence of special characters only in a textbox.!!!

            Takecare!




            jk

            Comment

            • gits
              Recognized Expert Moderator Expert
              • May 2007
              • 5388

              #7
              hi ...

              that's what you needed ... in't it :) ... post back to the forum anytime you have more questions ...

              kind regards

              Comment

              • jayakrishnanav
                New Member
                • Jan 2008
                • 10

                #8
                Originally posted by gits
                hi ...

                that's what you needed ... in't it :) ... post back to the forum anytime you have more questions ...

                kind regards

                hi gits,

                Yes thatz exactly wat i needed! Then i ll post my queries in dis forum,when i got any problems.


                WarmRegards
                jk

                Comment

                Working...