Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Javascript
|
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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 27th, 2003, 05:26 AM
Friend of Wrox
 
Join Date: Jun 2003
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
 
Old October 27th, 2003, 07:08 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
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
 
Old October 27th, 2003, 11:22 AM
Friend of Wrox
 
Join Date: Jun 2003
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
 
Old October 27th, 2003, 11:47 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
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
 
Old October 28th, 2003, 05:56 AM
Friend of Wrox
 
Join Date: Jun 2003
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
 
Old November 4th, 2003, 10:58 AM
Authorized User
 
Join Date: Jun 2003
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.
 
Old November 7th, 2003, 01:41 PM
Friend of Wrox
 
Join Date: Jun 2003
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
 
Old November 10th, 2003, 09:34 AM
Friend of Wrox
 
Join Date: Jun 2003
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!





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





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.