运行时的jquery验证

时间:2014-12-04 09:31:02

标签: javascript jquery jquery-validation-engine

我想在js文件中创建一个表单。

var form = '<form id="formForgetPass" action="#" method="POST" class="form-horizontal"><div class="form-group" style="margin-bottom: 0px !important;"><label for="textfield" class="control-label col-sm-2">E-posta</label><div class="col-sm-10"><input type="text" id="forgetEmail" name="email" class="form-control" /></div></div></form>';

我正在使用JquerValidation

我可以在运行时控制表单输入吗?我还没弄明白。

它无法正常工作,如下所示

    $("#formForgetPass").validate({
    rules: {
        email: {
            required: true,
            email: true
        }
    }
});

2 个答案:

答案 0 :(得分:0)

<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
  var form = '<form id="formForgetPass" action="#" method="POST" class="form-horizontal"><div class="form-group" style="margin-bottom: 0px !important;"><label for="textfield" class="control-label col-sm-2">E-posta</label><div class="col-sm-10"><input type="text" id="forgetEmail" name="email" class="form-control" /></div></div></form>';

$("#MainDiv").append(form);
alert($("#forgetEmail"));

});
</script>
</head>
<body>

<div id="MainDiv">
</div>

</body>
</html>

U可以附加到div元素并获取元素。此警报将返回对象。

答案 1 :(得分:0)

<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
  var form = '<form id="formForgetPass" action="#" method="POST" class="form-horizontal"><div class="form-group" style="margin-bottom: 0px !important;"><label for="textfield" class="control-label col-sm-2">E-

posta</label><div class="col-sm-10"><input type="text" id="forgetEmail" name="email" class="form-control" /></div></div></form>';

$("#MainDiv").append(form);
});

function uivalidate() {
    if  ($("#forgetEmail").val() =='' ) {
        alert(" Value Should not be empty !");
        return false;
    }
    alert('The value - ' +  $("#forgetEmail").val());
}
</script>
</head>
<body>

<div id="MainDiv">
</div>
<br>
<button type="button" onClick="uivalidate();">Click Me!</button>
</body>
</html>

    **Hi we can validate like this... this is question u r asking... give me the clarity**
相关问题