 |
| 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
|
|
|
|

October 27th, 2003, 05:26 AM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 347
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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
|
|

October 27th, 2003, 07:08 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
|
|
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
|
|

October 27th, 2003, 11:22 AM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 347
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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
|
|

October 27th, 2003, 11:47 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
|
|
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
|
|

October 28th, 2003, 05:56 AM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 347
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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
|
|

November 4th, 2003, 10:58 AM
|
|
Authorized User
|
|
Join Date: Jun 2003
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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.
|
|

November 7th, 2003, 01:41 PM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 347
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Great, thanks alot - will give it a go.
Adam
|
|

November 10th, 2003, 09:34 AM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 347
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Thanks alot - all good and working now!
|
|
 |