Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Senior Coder angst's Avatar
    Join Date
    Apr 2004
    Location
    Toronto, Ontario
    Posts
    2,114
    Thanks
    15
    Thanked 122 Times in 122 Posts

    Question Form validation on upload feild?

    Hi,

    I'm wondering how i can validate if a user has selected a file before hitting the upload button on an input=file field?

    like:
    <input type="file" name="userfile[0]">

    I've been looking around, but haven't found anything yet,

    any ideas?

    thanks in advance for your time!
    -Ken

  • #2
    Regular Coder martin_narg's Avatar
    Join Date
    Jul 2002
    Location
    Chamonix, France
    Posts
    600
    Thanks
    1
    Thanked 3 Times in 3 Posts
    Code:
    if(document.elements["userfile[0]"].value == "") {
      // .....
    }
    Hope this helps

    m_n
    "Cos it's strange isn't it. You stand in the middle of a library and go 'Aaaaaaaaaaaaaaaaggggggghhhhhhh!'
    and everybody just stares at you. But you do the same in an aeroplane, and everybody joins in."
    -Tommy Cooper

  • #3
    Senior Coder angst's Avatar
    Join Date
    Apr 2004
    Location
    Toronto, Ontario
    Posts
    2,114
    Thanks
    15
    Thanked 122 Times in 122 Posts
    sorry, i'm new to javascript,
    and i'm not sure what to do with this,

    will this check if a file has been select while hiting the submit button?

    thanks again for your time!
    -Ken

  • #4
    Regular Coder martin_narg's Avatar
    Join Date
    Jul 2002
    Location
    Chamonix, France
    Posts
    600
    Thanks
    1
    Thanked 3 Times in 3 Posts
    Above your closing </head> tag:
    Code:
    <script type="text/javascript">
    function validate(f) {
    	if(f.elements["userfile[0]"].value == "") {
    		alert("Please select a file");
    		return false;
    	}
    	return true;
    }
    </script>
    Your form tag:
    Code:
    <form name="PutYourFormNameHere" method="post" action="PutYourFormHandlerPageHere.asp" onsubmit="return validate(this);">
    Hope this helps

    m_n
    "Cos it's strange isn't it. You stand in the middle of a library and go 'Aaaaaaaaaaaaaaaaggggggghhhhhhh!'
    and everybody just stares at you. But you do the same in an aeroplane, and everybody joins in."
    -Tommy Cooper


  •  

    Posting Permissions

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