Click here to Skip to main content
15,921,463 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

In my webpage I am used Fileupload control and upload button also. I used javascript for validate selected file is image format or not. Whenever I press enter on the textbox control the alert popup is opened. But my requirement javascript function called only when I click upload button.

My javascript code
JavaScript
function validateFileUpload(obj) {
  

    var fileName = new String();
    var fileExtension = new String();

    // store the file name into the variable  
    fileName = obj.value;

    // extract and store the file extension into another variable  
    fileExtension = fileName.substr(fileName.length - 3, 3);

    // array of allowed file type extensions  
    var validFileExtensions = new Array("jpg", "png", "gif", "jpeg", "tif", "tiff", "ico", "bmp");

    var flag = false;

    // loop over the valid file extensions to compare them with uploaded file  
    for (var index = 0; index < validFileExtensions.length; index++) {
        if (fileExtension.toLowerCase() == validFileExtensions[index].toString().toLowerCase()) {
            flag = true;
        }
    }

    // display the alert message box according to the flag value  
    if (flag == false) {
        alert('Files with extension ".' + fileExtension.toUpperCase() + '" are not allowed.\n\nYou can upload the files with following extensions only:\n jpg, jpeg, bmp, ico, tif, tiff, png, gif\n');
        return false;
    }
    else {
        return true;
    }
}  
Posted
Updated 17-Dec-13 21:04pm
v3
Comments
Can you show the code?
devausha 18-Dec-13 3:04am    
See the updated question

C#
if(characterCode == 13)
{
    return false; 
}


include this in your javascript code
 
Share this answer
 
v2
$('#formid').bind("keyup keypress", function(e) {
var code = e.keyCode || e.which;
if (code == 13) {
e.preventDefault();
return false;
}
});
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900