More than one function per INPUT type ?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Craig

    #1

    More than one function per INPUT type ?

    I currently have the following code:

    <input name="castStati on" type="text" value="0"
    onChange="value Calculate()";>

    The function called does a few caluclations and writes the total to a
    total box.

    However, if the user type a non-numeric char, I get NaN.

    Is there a way to only allow numeric entry? I tried to add a
    onKeyPress, but the problem is I don't think you can have more than
    one function per <input..>, or can you?
  • Guido Wesdorp

    #2
    Re: More than one function per INPUT type ?

    Craig wrote:[color=blue]
    >
    > Is there a way to only allow numeric entry? I tried to add a
    > onKeyPress, but the problem is I don't think you can have more than
    > one function per <input..>, or can you?[/color]

    Sure you can... Just add the event handler to the HTML and it should
    work. I think using JavaScript you can actually register more than one
    function *per event type*...

    Cheers,

    Guido

    Comment

    • Michael Winter

      #3
      Re: More than one function per INPUT type ?

      Guido Wesdorp wrote on 02 Dec 2003:
      [color=blue]
      > Craig wrote:[color=green]
      >>
      >> Is there a way to only allow numeric entry? I tried to add a
      >> onKeyPress, but the problem is I don't think you can have more
      >> than one function per <input..>, or can you?[/color]
      >
      > Sure you can... Just add the event handler to the HTML and it
      > should work. I think using JavaScript you can actually register
      > more than one function *per event type*...[/color]

      Is this what you mean?

      <INPUT ... onclick="myFirs tFunction();myS econdFunction() ;[etc...]">

      In that example, myFirstFunction (), followed by mySecondFunctio n(),
      followed by...(until the end of the list) would be executed on each
      click.

      To Craig - a slight syntax error in your example:

      <input name="castStati on" type="text" value="0"
      onChange="value Calculate()";>
      ^
      That semi-colon shouldn't be there. It doesn't need to be present in
      the intrinsic event, either - only if there is more than one
      statement.

      The best way to cover this really is to just test for NaN and alert
      the user. There are too many avenues to cover that can interfere with
      other functionality when restricting entry. Instead, do something
      like this:

      // Returns true if is valid number, false otherwise
      //
      function isValidNumber( num ) {
      return !isNaN( Number( num )));
      }

      If you only want to validate integers, for example, you could use:

      function isValidInt( num ) {
      return !isNaN( parseInt( num )));
      }

      Mike

      --
      Michael Winter
      M.Winter@blueyo nder.co.uk.invalid (remove ".invalid" to reply)

      Comment

      • Dr John Stockton

        #4
        Re: More than one function per INPUT type ?

        JRS: In article <Xns9445A65BA3C 30MWinterBlueyo nder@193.38.113 .46>, seen
        in news:comp.lang. javascript, Michael Winter <M.Winter@bluey onder.co.uk.
        invalid> posted at Tue, 2 Dec 2003 16:21:13 :-[color=blue]
        >
        >If you only want to validate integers, for example, you could use:
        >
        >function isValidInt( num ) {
        > return !isNaN( parseInt( num )));
        >}[/color]

        One ) too many. Moreover, it accepts such as 0x77 and 3+3.

        Better to use a RegExp, especially if limiting the number of digits is
        good.

        Your num is in fact a string, and could be so named.

        function isValidInt(Str) { return /^\d{1,12}$/.test(Str) }

        for non-negative integers of 1..12 decimal digits.

        See <URL:http://www.merlyn.demo n.co.uk/js-valid.htm> and its links.

        --
        © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
        <URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang. javascript
        <URL:http://www.merlyn.demo n.co.uk/js-index.htm> JS maths, dates, sources.
        <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/JS/&c., FAQ topics, links.

        Comment

        Working...