Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

don't check empty values

Status
Not open for further replies.

lin73

Programmer
Feb 17, 2006
110
SE
I have this script that checks the extension of a file field, It works fine except for the fact that if I enter file that is not allowed and then remove the entered text, then I still get the message. How can I hange the script so I only get the alert message if there actually is some text in the file field?

var valid_extensions = /(.jpg|.jpeg|.gif|.txt|.rtf|.pdf|.ppt|.xls|.avi)$/i;
var alertmsg = "The selected file is of the wrong type an will not be uploaded \n allowed extensions are .jpg,.jpeg,.gif,.txt,.rtf,.pdf,.ppt,.xls,.avi";
//
function CheckExtension(fld) {
if (valid_extensions.test(fld.value)) return true;
alert(alertmsg);
fld.select();
fld.focus();
return false;
}
 
if (fld.value != "" && valid_extensions.test(fld.value))
 
Hi

I tryed this..

if (fld.value != "" && valid_extensions.test(fld.value)) return true;

But I still get the alert when I have removed the text and click on the page....
 
>How can I hange the script so I only get the alert message if there actually is some text in the file field?
I think then you mean you allow empty entry as well, so you might actually need this instead.
>if (fld.value != "" && valid_extensions.test(fld.value)) return true;
[tt]if (fld.value [red]=[/red]= "" [red]||[/red] valid_extensions.test(fld.value)) return true;[/tt]

Furthermore, your pattern should mean literal dot.
>var valid_extensions = /(.jpg|.jpeg|.gif|.txt|.rtf|.pdf|.ppt|.xls|.avi)$/i;
[tt]var valid_extensions = /([red]\[/red].jpg|[red]\[/red].jpeg|[red]\[/red].gif|[red]\[/red].txt|[red]\[/red].rtf|[red]\[/red].pdf|[red]\[/red].ppt|[red]\[/red].xls|[red]\[/red].avi)$/i;[/tt]
 
Sorry, I probably didn't read it right, try,


Code:
function CheckExtension(fld) {
    if(valid_extensions.test(fld.value))return true; 
    if(fld.value != "")
    alert(alertmsg);
    fld.select();
    fld.focus();
    return false;

}
also, I think the regexp should be like this,

Code:
var valid_extensions = /(\.jpg|\.jpeg|\.gif|\.txt|\.rtf|\.pdf|\.ppt|\.xls|\.avi)$/i;

and finally, you really want to check this server-side too.
 
Hi

Worked excellent, Thanks!
yes I allready do that check server side, but I wanted this one too.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top