PDA

View Full Version : Resolved Javascript validation onchange event



dragon_sa
03-03-2012, 03:24 PM
I have this javascript function that shows the submit button when a file is selected in a form.
What I would like to do with that is add to the function so it checks the value selected in the file field to see if the last 4 characters are .jpg or .JPG if so show the submit button if not show error message which can be a dive set to hidden also at the start if needed
How do I do that?

the form code that invokes the script is


<input name="image_file" type="file" size="35" id="image_file" accept="image/jpeg" onchange="showDiv('submitDiv');return true;" onkeypress="return nokeys(event)">


and the current javascript function is


<script>
function showDiv(objectID) {
var theElementStyle = document.getElementById(objectID);
theElementStyle.style.visibility = "visible";
}
</script>


Thanks for your help

vwphillips
03-03-2012, 04:14 PM
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>
<title></title>
<script>
function showDiv(objectID) {
var ip=document.getElementById('image_file'),txt=ip.value.split('.');
var theElementStyle = document.getElementById(objectID);
theElementStyle.style.visibility = "hidden";
if (txt.length==2&&(txt[1]=='jpg'||txt[1]=='JPG')){
theElementStyle.style.visibility = "visible";
alert('correct');
}
else {
alert('bad file');
}
}
</script>

</head>

<body>
<input name="image_file" type="file" size="35" id="image_file" accept="image/jpeg" onchange="showDiv('submitDiv');return true;" onkeypress="return nokeys(event)">
</body>

</html>

dragon_sa
03-04-2012, 01:56 AM
Thank you very much Vic, exactly what I was after :)