#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2013
    Posts
    16
    Rep Power
    0

    How to prevent a form submission without an attachment?


    need to validate before the form submit, whether the user selected the file for upload...

    without file attachment the form shouldn't be submitted. it will show a message to the user "please a select file to upload'...

    how to do it with javascript??

    its my form

    Code:
    <form method="POST" name="email" action="sendmail.php" enctype="multipart/form-data" onSubmit="return validateForm()";>
    
        <table border=0 cellPadding=5 cellSpacing=10 width=100%>
       
            <tr>
                <td><label for='name'>Name:</label></td>
                <td><input  type="text" name="name"  maxlength="50" size="30" ononkeyup="AllowAlphabet()"></td>
            </tr>
             <tr>
                <td><label for='familyname'>Family Name:</label></td>
                <td><input  type="text" name="fname" maxlength="50" size="30"></td>
            </tr>
             <tr>
                <td><label for='age'>Age:</label></td>
                <td><input  type="number" name="age" maxlength="50" size="30"></td>
            </tr>
            <tr>
                <td><label for='experience'>Years of Experience:</label></td>
                <td><input  type="number" name="exp" maxlength="50" size="30"></td>
            </tr>
            <tr>
                <td><label for='university'>University Attened:</label></td>
                <td><input  type="text" name="university" maxlength="50" size="30"></td>
            </tr>
             <tr>
                <td><label for='phone'>Phone Number:</label></td>
                <td><input  type="text" name="phone" maxlength="50" size="30"></td>
            </tr>
             <tr>
                <td><label for='email'>Email:</label></td>
                <td><input  type="email" name="email" maxlength="50" size="30"></td>
            </tr>
            <tr>
                <td><label for='resume'>Resume:</label></td>
                <td><input type="file" name="resume"></td>
            </tr>
            <tr>
                <td colSpan=2 align=center><input class=subbtn type="submit" value="Submit" name='submit' /></td>
            </tr>
            
        </table>
    </form>
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    26
    Rep Power
    0
    add this to the head section ...

    Code:
    <script>
    function validateForm(){return !!document.email.resume.value}
    </script>
    It is not the best or
    most modern style
    of coding but it prevents
    submission unless
    a file is selected.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2013
    Posts
    16
    Rep Power
    0
    Originally Posted by daveyerwin
    add this to the head section ...

    Code:
    <script>
    function validateForm(){return !!document.email.resume.value}
    </script>
    It is not the best or
    most modern style
    of coding but it prevents
    submission unless
    a file is selected.
    thank you!
  6. #4
  7. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    Hi,

    JavaScript validation doesn't prevent anything.

    The best it can do is assist well-meaning users who have JavaScript turned on. If the user doesn't like your validation, they can circumvent it any time. If JavaScript is turned off, the user may not even realize that the field is required. So there's absolutely no guarantee that you'll receive complete submission.

    If you want to force the users to fill out the field, you need server-side validation. This doesn't prevent people from uploading garbage data, of course.

    If you just wanna ask the user to fill out the field, it's generally better to use the required attribute, because it doesn't rely on JavaScript:

    html Code:
    <input type="file" name="some_file" required>
    The 6 worst sins of securityHow to (properly) access a MySQL database with PHP

    Why can’t I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2013
    Posts
    6
    Rep Power
    0
    Originally Posted by Jacques1
    Hi,

    JavaScript validation doesn't prevent anything.

    The best it can do is assist well-meaning users who have JavaScript turned on. If the user doesn't like your validation, they can circumvent it any time. If JavaScript is turned off, the user may not even realize that the field is required. So there's absolutely no guarantee that you'll receive complete submission.

    If you want to force the users to fill out the field, you need server-side validation. This doesn't prevent people from uploading garbage data, of course.

    If you just wanna ask the user to fill out the field, it's generally better to use the required attribute, because it doesn't rely on JavaScript:

    html Code:
    <input type="file" name="some_file" required>
    Yeah, good idea. And use server side validate for security.
  10. #6
  11. Devshed Beginner (1000 - 1499 posts)

    Join Date
    Jan 2004
    Location
    New Springfield, OH
    Posts
    1,236
    Rep Power
    1469
    Everyone has touched on this, but I'd like to elaborate a little bit.

    Jacques is correct is telling you to make the upload field required. The others are correct that you should not rely on Javascript validation.

    You should always perform backend validation. In other words, your application should be built to handle a submission without a file upload, even if that just means returning the user to the form with an error message. You should always anticipate that users will not do what you want them to and be prepared for anything. Your application should gracefully handle any state that you throw at it. In this case, that is a form submission with or without an attached file.

    Client-side code should only control behavior. In other words, you could use JavaScript to disable the submit button on page load, and then enable it when a file has been selected. (Notice that I did not say to make the button disabled by default, your backend is already handling that.)

    This creates a system that is both properly built, with code where it belongs, that downgrades nicely if JavaScript is disabled.

    Adding the required attribute takes things one step further and takes advantage of built-in browser capabilities. But again, while you should use this as well, you should not rely on it!
    Don't like me? Click it.

    Scripting problems? Windows questions? Ask the Windows Guru!

    Stay up to date with all of my latest content. Follow me on Twitter!

    Help us help you! Post your exact error message with these easy tips!

IMN logo majestic logo threadwatch logo seochat tools logo