Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Javascript
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
Javascript General Javascript discussions.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Javascript section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old October 27th, 2003, 05:26 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: , , .
Posts: 347
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to Adam H-W
Default No error but not working correctly!

Hi there

I've got this piece of javascript trying to validate whether the uploaded image is either a gif or jpg

function Validate(objForm)
{

    re = /^(file|C):\/\/\S+\/\S+\.(gif|jpg)$/i

    if (re.test(objForm.ImgURL.value)) {
    document.chgImg.src = objForm.ImgURL.value
    }
    else {
    alert("Image needs to be a gif or jpg.")
    objForm.ImgURL.focus()
    objForm.ImgURL.select()
    return false;
    }

However, it only gets to the alert - "Image needs to be a gif or jpg" and no further even when I do try to upload a jpg.

Can anyone point me in the right direction as to how to get this working right?

thanks

Adam
Reply With Quote
  #2 (permalink)  
Old October 27th, 2003, 07:08 AM
joefawcett's Avatar
Wrox Author
Points: 9,763, Level: 42
Points: 9,763, Level: 42 Points: 9,763, Level: 42 Points: 9,763, Level: 42
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Exeter, , United Kingdom.
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

Seems to work for me. Show us a bit more code and html. You should return true if the file name passes the test and maybe change the last part to (gif|jpg|jpeg) though.

--

Joe
Reply With Quote
  #3 (permalink)  
Old October 27th, 2003, 11:22 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: , , .
Posts: 347
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to Adam H-W
Default

Hi Joe

Here's the full script:

</SCRIPT>
<script language = "javascript">

function Validate(objForm)
{

    re = /^(file|C):\/\/\S+\/\S+\.(gif|jpg)$/i

    if (re.test(objForm.ImgURL.value)) {
    document.chgImg.src = objForm.ImgURL.value
    }
    else {
    alert("Image needs to be a gif or jpg.")
    objForm.ImgURL.focus()
    objForm.ImgURL.select()
    return false;
    }

    if (objForm.N.value == "")
    {
        window.alert("Name is a required Field!");
        objForm.N.focus();
        return false;
    }

    if (objForm.E.value == "")
    {
        window.alert("Email Address is a required Field!");
        objForm.E.focus();
        return false;
    }

    if (objForm.E.value.indexOf("@", 0) == -1 || (objForm.E.value.indexOf("@", 0) != objForm.E.value.lastIndexOf("@", (objForm.E.value.length - 1))) || objForm.E.value.indexOf(".", 0) == -1)
    {
        window.alert("Email Address must be a valid Email Address!");
        objForm.E.focus();
        return false;
    }

    return true;
}
// -->
</script>


thanks alot

Adam
Reply With Quote
  #4 (permalink)  
Old October 27th, 2003, 11:47 AM
joefawcett's Avatar
Wrox Author
Points: 9,763, Level: 42
Points: 9,763, Level: 42 Points: 9,763, Level: 42 Points: 9,763, Level: 42
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Exeter, , United Kingdom.
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

What is in ImgURL element? For your regular expression to work the path must be in the form:
c://myFolder/myFile.gif
It must have exactly one folder and have forward slashes as delimiters.



--

Joe
Reply With Quote
  #5 (permalink)  
Old October 28th, 2003, 05:56 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: , , .
Posts: 347
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to Adam H-W
Default

Hi Joe

This is the form element code:

<input name="ImgURL" type="file" class="box" id="ImgURL">

Can't see the wood from the trees at the moment as to how it needs to change to make it work.

thanks

Adam
Reply With Quote
  #6 (permalink)  
Old November 4th, 2003, 10:58 AM
Authorized User
 
Join Date: Jun 2003
Location: Madrid, Madrid, Spain.
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Here you have an alternative form to do it:):

 //URL of the selected file
 var tipo = objForm.ImgURL.value;
 tipo = tipo.substring(tipo.indexOf(".")+1);
 //Set of the types of archives that can select
 var tipos = new Array("gif","jpg","jpeg");
 var isImg = false;
 for(var i= 0; i<tipos.length; i++) {
     if(tipo == tipos[i]) {
        var oImagen = document.getElementById("chgImg");
        oImagen.src = objForm.ImgURL.value
    isImg = true;
    break;
     }
 }
 if(!isImg){
    alert("Image needs to be a gif or jpg.");
    objForm.ImgURL.focus()
    objForm.ImgURL.select()
    return false;
 }

I hope that it serves to you as aid.
Reply With Quote
  #7 (permalink)  
Old November 7th, 2003, 01:41 PM
Friend of Wrox
 
Join Date: Jun 2003
Location: , , .
Posts: 347
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to Adam H-W
Default

Great, thanks alot - will give it a go.

Adam
Reply With Quote
  #8 (permalink)  
Old November 10th, 2003, 09:34 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: , , .
Posts: 347
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to Adam H-W
Default

Thanks alot - all good and working now!
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Contains function call not working correctly cnatale XSLT 7 May 8th, 2008 07:53 AM
Hello User, not working correctly? Neotriple BOOK: Beginning Visual Basic 2005 ISBN: 978-0-7645-7401-6 1 October 5th, 2006 11:39 AM
Proxxy Connections working, but not correctly??? ramangill VB.NET 0 August 10th, 2005 06:54 AM
Proxxy Connections working, but not correctly??? ramangill Oracle 0 August 10th, 2005 06:53 AM
document.LastModified not working correctly in php Annoyamouse PHP How-To 1 December 7th, 2004 10:04 AM



All times are GMT -4. The time now is 04:46 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.