验证动态创建的字段?

时间:2013-07-23 06:03:46

标签: javascript jquery validation dynamic

有人可以请我指向这个代码段的正确方向吗?

function validateForm() {
    var flag=1;
//i have array containg values some values array[]
    $(document).ready(function () {
      alert("what happened");
      //my form contains dynamically generated input fields..
     //i can't seem to generate the ids.. 
    var y=$("#inputfield"+array[1]).val();// seems to me like error in this line
    //{validation code using y}
    flag = 0;
});
if (flag === 0) {

    return false;
}

}

这是html部分:

<form action="L4.php" method="post" onSubmit="return validateForm();">
<input type="submit">
</form>

2 个答案:

答案 0 :(得分:0)

  var input= $('input').filter(function(){ return this.id.match("input_type");});

    $.each(input, function(i,obj){
            var id = "#"+$(input[i]).attr('id');
                    if(id.match(/Fname/)){
                          //do something
                    }else if(id.match(/Lname/)){
                         //do something  
                    }
        });

答案 1 :(得分:0)

试试这个,

function validateForm() {
    var flag=1;
    $('form input[type="text"]').each(function(){//looping for all textboxes
        if($(this).val()=="")//if the textbox is empty
            flag=0;
    });
    if (flag === 0) {
        alert('error');
        return false;
    }
    return true;// if ok then return true
}