如何在codeigniter中提交之前验证表单

时间:2015-02-28 09:47:46

标签: jquery forms codeigniter client-side-validation

我想在codeigniter中使用jquery在提交之前验证a,但我很困惑如何在提交之前验证表单。 我知道codeigniter中的验证类,但我想在前端验证一个表单。

1 个答案:

答案 0 :(得分:5)

我使用bootstrap验证器进行客户端验证。易于使用和理解。

这是一个样本。

<form id="signinForm">
    <div class="form-group">
        <input type="text" class="form-control" name="username" placeholder="Username" />
    </div>

    <div class="form-group">
        <input type="password" class="form-control" name="password" placeholder="Password" />
    </div>

    <!-- Do NOT use name="submit" or id="submit" for the Submit button -->
    <button type="submit" class="btn btn-default">Sign in</button>
</form>

<script>
$(document).ready(function() {
    $('#signinForm').formValidation({
        framework: 'bootstrap',
        icon: {
            valid: 'glyphicon glyphicon-ok',
            invalid: 'glyphicon glyphicon-remove',
            validating: 'glyphicon glyphicon-refresh'
        },
        fields: {
            username: {
                validators: {
                    notEmpty: {
                        message: 'The username is required'
                    },
                    stringLength: {
                        min: 6,
                        max: 30,
                        message: 'The username must be more than 6 and less than 30 characters long'
                    },
                    regexp: {
                        regexp: /^[a-zA-Z0-9_]+$/,
                        message: 'The username can only consist of alphabetical, number and underscore'
                    }
                }
            },
            password: {
                validators: {
                    notEmpty: {
                        message: 'The password is required'
                    }
                }
            }
        }
    });
});
</script>

使用此link作为参考。