每当我按下所有文本框控件上的输入按钮时,都会显示javascript警告消息 [英] javascript alert message shown whenever I pressed enter button on all the textbox control

查看:60
本文介绍了每当我按下所有文本框控件上的输入按钮时,都会显示javascript警告消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





在我的网页中,我也使用了Fileupload控件和上传按钮。我使用javascript验证所选文件是否是图像格式。每当我按下文本框控件上的Enter键时,都会打开警告弹出窗口。但是我的要求只在我点击上传按钮时调用javascript函数。



我的javascript代码

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

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

推荐答案

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





在您的javascript代码中包含此内容



include this in your javascript code



('#formid')。bind(keyup keypress,function(e){

var code = e.keyCode || e.which;

if(code == 13){

e.preventDefault();

返回false;

}

});
('#formid').bind("keyup keypress", function(e) {
var code = e.keyCode || e.which;
if (code == 13) {
e.preventDefault();
return false;
}
});


这篇关于每当我按下所有文本框控件上的输入按钮时,都会显示javascript警告消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆