-4

i want to validate a from before submit using jquery in codeigniter but i am confused how to validate a form before submit. I know the validation class in codeigniter but i want validate a form in front end.

Sparky
  • 98,165
  • 25
  • 199
  • 285
Shehzad Ahmed
  • 21
  • 1
  • 2
  • 12

1 Answers1

5

I use bootstrap validator for client side validations. Easy to use & understand.

Here's a sample.

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

Use this link for reference.

version 2
  • 1,049
  • 3
  • 15
  • 36