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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Clear input box after clearing alert message

Status
Not open for further replies.

jruiseco

Programmer
Oct 10, 2001
52
0
0
US
I have a file input box that gets validated by javascript. I am testing the extension of the file to determine if it is a pdf. If not an alert box should be thrown.

After the alert box is thrown and the user clicks OK I want the input field to clear and the bad value removed.

Here is what I have so far:

function check(file) {
ext = file.substring(file.lastIndexOf('.')+1);
if (ext != "pdf") {
alert("You must upload document in Adobe PDF format...");
event.returnValue=false; }
}

<input type=&quot;File&quot; name=&quot;attach1&quot; onpropertychange=&quot;check(this.value);&quot;>

How do I clear the field?
 
the user clicks OK I want the input field to clear and the bad value removed.

<input type=&quot;File&quot; name=&quot;attach1&quot; id=&quot;attach1&quot; onpropertychange=&quot;check(this.value);&quot;>

Use - document.getElementById(&quot;attach1&quot;).value = &quot;&quot;;

Be sure to put id=&quot;attach1&quot; in the input statement!

Good Luck! [thumbsup2]
WindUp
 
I'm not very good at this...

Here's what I have but it's not working. What's wrong?

function check(file,idv) {
ext = file.substring(file.lastIndexOf('.')+1);
if (ext != &quot;pdf&quot;) {
alert(&quot;You must upload document in Adobe PDF format...&quot;);
event.returnValue=false;
document.getElementById(&quot;idv&quot;).value = &quot;&quot;;
}
}
 
you cannot change the value of an input type=file with javascript.



=========================================================
try { succeed(); } catch(E) { tryAgain(); }
-jeff
 
Jeff's right, it'd be a security issue if you could. The only thing you can do is use .reset() on the form.

Adam
while(ignorance==true){perpetuate(violence,fear,hatred);life--};
 
OK. Let me ask a different question...

How do I write a function that will be called on form submit that will examine all the type=file fields and when it finds files that do not have a pdf extension will throw an alert and name the offending fields for the user?
 
How do I write a function that will be called on form submit that will examine all the type=file fields and when it finds files that do not have a pdf extension will throw an alert and name the offending fields for the user?

Try something like this...

<input type=&quot;File&quot; name=&quot;attach1&quot; id=&quot;attach1&quot;>

<input type=&quot;submit&quot; name=&quot;submit&quot; id=&quot;submit&quot; value=&quot;Submit&quot; onClick='javascript:return verifyFields();'>&quot;)

<script language=JavaScript>
function verifyFields()
{ //*** Verify Attach1 Field ***//
if ( /$\.pdf/i.test(document.getElementById(&quot;attach1&quot;).value) != true )
{ alert(&quot;The File MUST be a .pdf File.\nPlease select a pdf file.&quot;);
document.getElementById(&quot;attach1&quot;).focus();
return false;
}
</script>


Good Luck! [thumbsup2]
WindUp
 
Thank you to everyone for your help...

Regards!
 
I finally figured out how to clear the contents of a file element. You basically have to recreated it:

var f = document.formName.fileFieldName;
f.outerHTML=f.outerHTML;

Adam
while(woman.width>woman.height && wallet.value>0){beer++;vision.blur()};
 
adam, what about your original idea of calling reset() on the form? works fine for me:

<form>
<input type=&quot;file&quot;/>
<input type=&quot;button&quot; value=&quot;reset&quot; onclick=&quot;this.form.reset();&quot;/>
</form>


=========================================================
try { succeed(); } catch(E) { tryAgain(); }
-jeff
 
That works, but I wouldn't want to fill out a huge form from scratch just because I clicked the wrong file type.

Adam
while(woman.width>woman.height && wallet.value>0){beer++;vision.blur()};
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top