SitePoint Sponsor

User Tag List

Results 1 to 2 of 2

Thread: number, please

  1. #1
    Get my greedy down dotJoon's Avatar
    Join Date
    Apr 2003
    Location
    daejeon, South Korea
    Posts
    2,209
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    number, please

    Code:
    <script type = 'text/javascript'> 
    function emptyAlert(empty) { 
    if (empty['myText'].value.length == 0) {
    alert('Say something, please'); 
    return false 
    } 
    }
    </script>
    
     <form action="action.php" method="post" onsubmit="return emptyAlert(this)">
    <input type="text" name="myText" >
    <input type="submit">
    </form>
    If a user, with the code above, clicks the submit button without any text in the form, it says "Say something, please".

    I like to make it like the following.
    If a user clicks the submit button with any text which is not pure numeric value, it says "number, please."

    If a user clicks the submit button without any text, it says "number, please."


    If a user clicks the submit button with "3cc45" in myText , it says "number, please.

    If a user clicks the submit button with "abc" in myText , it says "number, please.


    If a user clicks the submit button with "345" in myText , it goes to action.php.

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,526
    Mentioned
    83 Post(s)
    Tagged
    3 Thread(s)
    Quote Originally Posted by dotJoon View Post
    [code]<script type = 'text/javascript'>
    function emptyAlert(empty) {
    if (empty['myText'].value.length == 0) {
    alert('Say something, please');
    return false
    }
    }
    </script>

    <form action="action.php" method="post" onsubmit="return emptyAlert(this)">
    <input type="text" name="myText" >If a user clicks the submit button with any text which is not pure numeric value, it says "number, please."
    If a user clicks the submit button without any text, it says "number, please."
    If a user clicks the submit button with "3cc45" in myText , it says "number, please.
    If a user clicks the submit button with "abc" in myText , it says "number, please.
    Then change the condition to something else that checks for a number.

    You can use !isNaN(Number(someString)) to perform such checks.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •