SitePoint Sponsor

User Tag List

Results 1 to 13 of 13
  1. #1
    Afrika
    Join Date
    Jul 2004
    Location
    Nigeria
    Posts
    1,737
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    text field validation

    hello,
    I need a simple text field validation scirpt that disallows a user from entering a value starting with 0 in the text field box

    if he does, it should prompt him and return to the default value

    thanks
    Afrika

    Code:
    <html>
    
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
    <title>New Page 1</title>
    </head>
    
    <body>
    
    <form method="POST" action="index.asp">
    	
    	<p><input type="text" name="phone_num" size="20" value="23480"><input type="submit" value="Submit" name="B1"></p>
    </form>
    
    </body>
    
    </html>

  2. #2
    SitePoint Addict
    Join Date
    May 2000
    Posts
    313
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    using the onSubmit event in your form tag - onSubmit="return Validate()"

    call the validate function;
    Code:
    function Validate()
     {
      if(document.forms[0].phone_num.value.substr(0,1)==0)
        {
         alert("can not start with zero")
         document.forms[0].phone_num.value="23480"
         return false
        }
     }

  3. #3
    Afrika
    Join Date
    Jul 2004
    Location
    Nigeria
    Posts
    1,737
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hello requestcode
    its not working.

    Am i doing something wrong ?

    <html>
    <SCRIPT language=javascript>

    function Validate()
    {
    if(document.forms[0].phone_num.value.substr(0,1)==0)
    {
    alert("can not start with zero")
    document.forms[0].phone_num.value="23480"
    return false
    }
    }
    </script>

    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
    <title>New Page 2</title>
    </head>

    <body>

    <form method="POST" action="index.asp">

    <p><input type="text" name="T1" size="20" value="23480"><input type="submit" onSubmit="return Validate()" value="Submit" name="B1"><input type="reset" value="Reset" name="B2"></p>
    </form>

    </body>

    </html>

  4. #4
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Nelson BC
    Posts
    2,310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you don't have a phone_num field on your form.

  5. #5
    Afrika
    Join Date
    Jul 2004
    Location
    Nigeria
    Posts
    1,737
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks Jim,
    i just changed it but still no prompt.

    What am i doing wrong here ?

    thanks
    Code:
    <html>
    <SCRIPT language=javascript>
    
    function Validate()
    {
    if(document.forms[0].phone_num.value.substr(0,1)==0)
    {
    alert("can not start with zero")
    document.forms[0].phone_num.value="23480"
    return false
    }
    }
    </script>
    
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
    <title>New Page 2</title>
    </head>
    
    <body>
    
    <form method="POST" action="index.asp">
    
    <p><input type="text" name="phone_nu" size="20" value="23480"><input type="submit" onSubmit="return Validate()" value="Submit" name="B1"><input type="reset" value="Reset" name="B2"></p>
    </form>
    
    </body>
    
    </html>
    Last edited by afrika; Feb 19, 2005 at 05:58.

  6. #6
    SitePoint Addict rokc's Avatar
    Join Date
    Oct 2004
    Posts
    280
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The onSubmit Event is called on the submission of a form and not on the click event of a button. So u need to specify onSubmit="return Validate()" inside the form tag. The same function can also be called onClick event of the Submit button.

    below the correct code...(by the way the field name u specified in the function for the phone number field was also incorrect.)
    HTML Code:
    <html>
    <SCRIPT language=javascript>
    
    function Validate()
    {
    if(document.forms[0].phone_nu.value.substr(0,1)==0)
    {
    alert("can not start with zero")
    document.forms[0].phone_nu.value="23480"
    return false
    }
    }
    </script>
    
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
    <title>New Page 2</title>
    </head>
    
    <body>
    
    <form method="POST" name="test" action="index.asp" onSubmit="return Validate();">
    
    <p><input type="text" name="phone_nu" size="20" value="23480"><input type="submit" value="Submit" name="B1"><input type="reset" value="Reset" name="B2"></p>
    </form>
    
    </body>
    </html>

  7. #7
    Afrika
    Join Date
    Jul 2004
    Location
    Nigeria
    Posts
    1,737
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hello Rokc,
    why shoudl i believe you? when u say you are at "Devil's liar"?

    Just joking, thanks a lot. It works well and just want i needed.

    afrika

  8. #8
    SitePoint Addict rokc's Avatar
    Join Date
    Oct 2004
    Posts
    280
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by afrika
    hello Rokc,
    why shoudl i believe you? when u say you are at "Devil's liar"?

    Just joking, thanks a lot. It works well and just want i needed.

    afrika
    Hi afrika,
    Its not Devil's Liar....
    Its Devil's Lair...

    by the way feels gr8 tht it worked

  9. #9
    Afrika
    Join Date
    Jul 2004
    Location
    Nigeria
    Posts
    1,737
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks a lot, i just posted another comment.

    I already have a function onsubmit in my form, so i was wondering how i could implement the two function

    <form action="confirm_send.asp" method="post" name= "sendsms_1newday" id="sendsms_1newday"onsubmit="this.elements['submit'].disabled = true;" onSubmit="return Validate();"></p>
    how do i add the second ?

    thanks

  10. #10
    SitePoint Addict rokc's Avatar
    Join Date
    Oct 2004
    Posts
    280
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hi,
    If i had to use 2 functions when the same event fires. I would call the second function from the first function. like this.

    function Validate()
    {
    if(document.forms[0].phone_nu.value.substr(0,1)==0)
    {
    alert("can not start with zero")
    document.forms[0].phone_nu.value="23480";
    test();
    return false;
    }
    }

    function test(){
    document.forms[0].elements['B1'].disabled = true;
    return false;
    }

  11. #11
    Afrika
    Join Date
    Jul 2004
    Location
    Nigeria
    Posts
    1,737
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks, but somewhere down the line ...

    only the alert works. However;

    1. the form disabler, disable it, once click.
    Its supposed to disable it when the parameters are correct.

    ie its supposed to make sure there is no 0 then disable and send the form.
    not alert and disable

    2. It submits the form, which is not supposed to be so

    any assistance, i really am not a javascript programmer

    rgds
    Afrika

  12. #12
    SitePoint Addict rokc's Avatar
    Join Date
    Oct 2004
    Posts
    280
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    HTML Code:
    function Validate()
    {
    if(document.forms[0].phone_nu.value.substr(0,1)==0)
    {
    alert("can not start with zero")
    document.forms[0].phone_nu.value="23480";
    return false;
    }
    else
    test();
    }
    
    function test(){
    document.forms[0].elements['B1'].disabled = true;
    return true;
    }

  13. #13
    Afrika
    Join Date
    Jul 2004
    Location
    Nigeria
    Posts
    1,737
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks a lot Rokc
    Afrika


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
  •