简体   繁体   English

验证控件的这段代码有什么问题?

[英]what is wrong in this code for validation of controls?

I use jquery for validation of controls: 我使用jquery来验证控件:

function RegisterClient() {
    var bookname = true; var edition = true; var author = true; var price = true; var imgType = true; var imgSize = true;

    if (j('#txt_book_name').val() === null) {   //1
        j('#alertBookName').removeClass('hidden');
        bookname = false;
    }
    if (j('#txt_edition').val() === null) {     //2
        j('#alertEditionbook').removeClass('hidden'); edition = false;
    }
    if (j('#txt_author').val() === null) {       //3
        j('#alertAuthor').removeClass('hidden'); author = false;
    }
    if (j('#txt_price').val() === null) {        //4
        j('#alertPrice').removeClass('hidden'); price = false;
    }
    var fileType = j("#ContentPlaceHolder1_FileUpload1").val().split('.').pop().toLowerCase();
    if (j('#ContentPlaceHolder1_FileUpload1').val() != "" && j.inArray(fileType, ['gif', 'png', 'jpg']) == -1)
    { j('#alertImgType').removeClass('hidden'); imgType = false; }
    if (j('#ContentPlaceHolder1_FileUpload1').val() != null) {
        var sizeImg = j("#ContentPlaceHolder1_FileUpload1")[0].files[0].size/1024;
        if (sizeImg > 25) { j('#alertImgSize').removeClass('hidden'); imgSize = false; }
    }
    if (bookname && edition && author && price && imgType && imgSize) __doPostBack('ctl00$ContentPlaceHolder1$Button1', '');

}
  1. In sections 1,2,3,4, the (j('#txt_...').val() === null) statement does not work correctly. 在第1,2,3,4节中, (j('#txt_...').val() === null)语句无法正常工作。

  2. The (var sizeImg = j("#ContentPlaceHolder1_FileUpload1")[0].files[0].size/1024;) statement has an error, shown when using firebug. (var sizeImg = j("#ContentPlaceHolder1_FileUpload1")[0].files[0].size/1024;)语句出错,在使用Firebug时显示。 I can show the value of j("#ContentPlaceHolder1_FileUpload1")[0].files[0].size with Alert() , but there is an error. 我可以使用Alert()显示j("#ContentPlaceHolder1_FileUpload1")[0].files[0].size ,但是有错误。

Regarding your first problem: 关于第一个问题:

The only that this is possible is that if condition gets executed before the user inputs anything. 唯一可能的是,如果条件在用户输入任何内容之前被执行。

I have made a fiddle to explain it: 我做了一个小提琴来解释它:

https://jsfiddle.net/tr91Lw7y/ https://jsfiddle.net/tr91Lw7y/

Run this and you will see "johndoe" instantly. 运行此命令,您将立即看到“ johndoe”。

But if you run this: 但是,如果运行此命令:

http://jsfiddle.net/6mjgx2r4/ http://jsfiddle.net/6mjgx2r4/

You will see blank anytime you do this. 每次执行此操作时,您都会看到空白。

There is one solution to your problem if you cannot adjust the flow: 如果您无法调整流量,则有一种解决方案:

$("#txt_book_name").keyup(function(){
        if($(this).val() === null) {
         // Conditions
        }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM