Results 1 to 3 of 3

Thread: Javascript validation onchange event

  1. #1
    Join Date
    Apr 2008
    Posts
    28
    Thanks
    8
    Thanked 1 Time in 1 Post

    Question Javascript validation onchange event

    I have this javascript function that shows the submit button when a file is selected in a form.
    What I would like to do with that is add to the function so it checks the value selected in the file field to see if the last 4 characters are .jpg or .JPG if so show the submit button if not show error message which can be a dive set to hidden also at the start if needed
    How do I do that?

    the form code that invokes the script is
    Code:
    <input name="image_file" type="file" size="35" id="image_file" accept="image/jpeg" onchange="showDiv('submitDiv');return true;" onkeypress="return nokeys(event)">
    and the current javascript function is
    Code:
    <script>
    function showDiv(objectID) {
    	var theElementStyle = document.getElementById(objectID);
    		theElementStyle.style.visibility = "visible";
    }
    </script>
    Thanks for your help
    Last edited by dragon_sa; 03-04-2012 at 01:54 AM.

  2. #2
    Join Date
    Dec 2008
    Location
    Portsmouth, UK
    Posts
    1,891
    Thanks
    2
    Thanked 441 Times in 435 Posts

    Default

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    
    <head>
      <title></title>
    <script>
    function showDiv(objectID) {
     var ip=document.getElementById('image_file'),txt=ip.value.split('.');
     var theElementStyle = document.getElementById(objectID);
     theElementStyle.style.visibility = "hidden";
     if (txt.length==2&&(txt[1]=='jpg'||txt[1]=='JPG')){
      theElementStyle.style.visibility = "visible";
      alert('correct');
     }
     else {
      alert('bad file');
     }
    }
    </script>
    
    </head>
    
    <body>
    <input name="image_file" type="file" size="35" id="image_file" accept="image/jpeg" onchange="showDiv('submitDiv');return true;" onkeypress="return nokeys(event)">
    </body>
    
    </html>
    Vic
    God Loves You and will never love you less.
    http://www.vicsjavascripts.org/Home.htm
    If my post has been useful please donate to http://www.operationsmile.org.uk/

  3. The Following User Says Thank You to vwphillips For This Useful Post:

    dragon_sa (03-04-2012)

  4. #3
    Join Date
    Apr 2008
    Posts
    28
    Thanks
    8
    Thanked 1 Time in 1 Post

    Default

    Thank you very much Vic, exactly what I was after

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
  •