SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Zealot
    Join Date
    Jan 2005
    Location
    London
    Posts
    186
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Cool Evaluate last 4 characters of file name using javascript

    Hi There

    I have built a page on a recruitment website which allows users to upload their CV via ASPUpload i.e a browse button. However I want to limit the file type to just MS Word documents as I do want them uploading large PowerPoint files etc. My idea is to have some javascript which evaluates the last 4 characters of the file name to ensure it is .doc
    So if they try to upload anything other than a .doc file a dialogue box will appear stating that they must only upload MS Word Document (when they click the ‘Upload CV’ button). Does anyone know how I can evaluate the last 4 characters of the file name? Once the user has browsed to their CV on their PC the path & file name will appear in a text field called ‘CV’.
    Has anyone done this before? Any help would be greatly appreciated.

    Many thanks

  2. #2
    SitePoint Wizard Pepejeria's Avatar
    Join Date
    Jan 2005
    Location
    Too far up north
    Posts
    1,566
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can get the last 4 characters of the fields value by using
    Code JavaScript:
    var lastFour = yourString.substr(-4);
    You can then have a simple check to see if the lastFour variable is equals to ".doc"

  3. #3
    SitePoint Zealot
    Join Date
    Dec 2007
    Posts
    120
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    <html>
    <head>

    <script type="text/javascript">
    function checkExtension() {
    var ext = document.myForm.upload.value;
    ext = ext.substring(ext.length-4,ext.length);
    ext = ext.toLowerCase();
    if(ext != ".doc") {
    alert("You selected a "+ext+" file\nPlease select a .doc file instead");
    return false;
    }
    else
    alert("Nice .doc file");
    return true;
    }
    </script>

    </head>
    <body>

    <form method="post" name="myForm" enctype="multipart/form-data" onsubmit="return checkExtension();" action="">
    <p>

    Please select a .doc file<br>
    <input type="file" name="upload" size="80" accept="image/jpeg">
    <p>
    <input type="submit" value="Upload This Bad Boy">

    </form>


    </body>
    </html>

  4. #4
    SitePoint Zealot
    Join Date
    Jan 2005
    Location
    London
    Posts
    186
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Cool

    just tried the code and it works perfectly - thank you very much for your help on this.

  5. #5
    SitePoint Zealot
    Join Date
    Jan 2005
    Location
    London
    Posts
    186
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Cool

    Hi There

    I added in the checkExtension() function and that worked fine within my form. However I am also trying to run the checkform () function, which validates the name field in the form. It does work to an extent, but when I click OK to the first warning dialogue box from the checkform () function it automatically submits the form. Rather than submit it should actually evaluate against the checkExtension() function instead, which would display a warning dialogue if someone tries to add a file that does not have a .doc file extension.

    The issue started to occur as soon as I added an extra piece of code which evaluates whether the ‘file’ field is blank or not. The file filed is not mandatory, so the form can be submitted with nothing in there. However if the field is blank it will keep showing the warning dialogue box for checkExtension() as it can’t find an extension to validate against. I think it is to do with the way I have added the extra if statement, but I not sure – just wondered if anyone can see where I am perhaps going wrong?

    Many thanks in advance for your help.

    Code is below:

    ------------------------------------------------------------------------

    function checkform (form)
    {

    if (form.ApplicantsName.value == "Full Name")
    {
    alert( "Please enter your 'Full Name'." );
    form.ApplicantsName.focus();
    return false ;
    }

    return true;
    }


    function checkExtension()
    {
    var ext = form.file.value;
    ext = ext.substring(ext.length-4,ext.length);
    ext = ext.toLowerCase();

    if((ext != ".doc") && (form.file.value != ""))

    {
    alert("You have tried to upload a "+ext+" file\nWe only accept CVs in MS Word format (.doc)\nPlease upload a .doc file\n Thank You.");
    return false;
    }
    else

    return true;
    }

    ---------------------------------------------------------------------------------------------
    <form action="test.asp " method="POST" enctype="multipart/form-data" name="form" id=form onsubmit="return checkform(form), checkExtension();validateForm(this, 'doc', true);;return document.MM_returnValue;">

    <input name="ApplicantsName" type="text" id="ApplicantsName" value="Full Name" size="30" onFocus="if(this.value==this.defaultValue)this.value=''"
    onBlur="if(this.value=='')this.value=this.defaultValue">

    <input name="file" type="file" class="maintext" onchange="validateFile(this, 'doc', true);return document.MM_returnValue" size="18" onFocus="if(this.value==this.defaultValue)this.value=''" onBlur="if(this.value=='')this.value=this.defaultValue" >
    <input type="submit" name="Submit" value="Send CV" onClick="MM_validateForm('ApplicantsName','','R','ApplicantsTelephone','','R','RisEmail');return document.MM_returnValue" />
    </form>
    ---------------------------------------------------------------------------------------------

  6. #6
    SitePoint Zealot
    Join Date
    Jan 2005
    Location
    London
    Posts
    186
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Cool

    Hi There

    I was just wondering if anyone had managed to take a quick glance at my posting above. Any pointers wouldl be a great help.

    many thanks in advance.

  7. #7
    SitePoint Wizard Pepejeria's Avatar
    Join Date
    Jan 2005
    Location
    Too far up north
    Posts
    1,566
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Use only one function in the onsubmit handler. You know have 3 functions and only the first one returns a value. So only the first one will actually affect if the form is submitted or not. The other functions will run too, but they will not hinder the form from being submitted.

    And... check out the substr function snippet above, substring is not really necessary here.


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
  •