SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Guru
    Join Date
    Oct 1999
    Location
    New York, NY
    Posts
    826
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Arrow simple form validator needed!

    Hi --

    I have a form and am using a php form script to allow my visitors to upload a file and for some reason, in the hidden "required" field, it doesn't register if the "file" line is missing and still allows the user to submit the form.

    Can someone please provide the code I'd use to have a simple alert box pop up if they don't fill in the file field, and continue if they did?!

    Thanks.

    Josh

  2. #2
    What? Maelstrom's Avatar
    Join Date
    Oct 2001
    Location
    Whistler BC originally from Guelph Ontario
    Posts
    2,175
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Something like this works well

    PHP Code:
    <script language="javascript1.2">
        <!--
            function 
    validate(formObj)
            {
                if (
    formObj.temp.length!=0)
                {                
                    
    alert ("Your field isn't empty");
                    return 
    false;
                }
                return 
    true;
            }
        
    //-->
        
    </script>
    </head>

    <body>
    <form action="test.htm" name="form1" id="form1" onsubmit="return validate(this)">
    <input type="text" name="temp">
    <input type="submit" name="submit" value="submit">
    </form> 
    Maelstrom Personal - Apparition Visions
    Development - PhP || Mysql || Zend || Devshed
    Unix - FreeBSD || FreeBsdForums || Man Pages
    They made me a sitepoint Mentor - Feel free to PM me or Email me and I will see if I can help.

  3. #3
    Currently Occupied; Till Sunda Andrew-J2000's Avatar
    Join Date
    Aug 2001
    Location
    London
    Posts
    2,475
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    check this server side as well with php, just in case the user disables javascript.

    Maelstrom your code will not work properly, your thinking backwards today:P

    PHP Code:

    if (formObj.temp.length!=0)
                {                
                    
    alert ("Your field isn't empty");
                    return 
    false;
                }
                return 
    true
    just inverse the return true and false statements.

    heres the other way

    PHP Code:
    <script language="javascript">
    <!--
            function 
    validate(formObj)
            {
            if (
    formObj.temp.value.length==0)
            {                
                
    alert ("Your field is empty");
                return 
    false;
            }
            
            
    with (document.form1)
            {
                
    method="get"
                
    action="testing.html";
                
    submit();
            }
            return 
    true;
        }
    //-->
    </script>
    </head>

    <body>
    <form name="form1" id="form1" onsubmit="return validate(this)">
    <input type="text" name="temp">
    <input type="submit" name="submit" value="submit">
    </form> 
    or

    PHP Code:

    <script language="javascript">
    <!--
            function 
    validate(formObj)
            {
            if (
    formObj.temp.value =='' || formObj.temp.value ==null)
            {                
                
    alert ("Your field is empty");
                return 
    false;
            }
            
            
    with (document.form1)
            {
                
    method="get"
                
    action="testing.html";
                
    submit();
            }
            return 
    true;
        }
    //-->
    </script>
    </head>

    <body>
    <form name="form1" id="form1" onsubmit="return validate(this)">
    <input type="text" name="temp">
    <input type="submit" name="submit" value="submit">
    </form> 
    <!-- edited - lol i just did the same in my last example Maelstrom, lol its catching :P
    Last edited by Andrew-J2000; Apr 15, 2002 at 23:33.

  4. #4
    What? Maelstrom's Avatar
    Join Date
    Oct 2001
    Location
    Whistler BC originally from Guelph Ontario
    Posts
    2,175
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    haha...yeah I did didn't I. Yes I am thinking backwards it seems. ...
    Maelstrom Personal - Apparition Visions
    Development - PhP || Mysql || Zend || Devshed
    Unix - FreeBSD || FreeBsdForums || Man Pages
    They made me a sitepoint Mentor - Feel free to PM me or Email me and I will see if I can help.


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
  •