Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

form validation using ajax with regex

Right now i am using ajax to get the value from input field on key press and match it with the data in mysql,if the data is unique then it will make the border green and mark tick else red border and cross. now i want to add regex to validate the input and restrict it.

function username_check() {
        var username = $('#warden_id').val();
        if (username == "" || username.length < 4) {
            $('#warden_id').css('border', '1px #CCC solid');
            $('#tick').hide();
        } else {

            jQuery.ajax({
                type: "POST",
                url: "check.php",
                data: 'username=' + username,
                cache: false,
                success: function (response) {
                    if (response == 1) {
                        $('#warden_id').css('border', '1px #C33 solid');
                        $('#tick').hide();
                        $('#cross').fadeIn();
                    } else {
                        $('#warden_id').css('border', '1px #090 solid');
                        $('#cross').hide();
                        $('#tick').fadeIn();
                    }

                }
            });
        }

regex i want to use to validate data

/^[a-zA-Z\s]+$/
like image 226
Muhammad Mannan Masood Avatar asked Oct 01 '15 01:10

Muhammad Mannan Masood


People also ask

How to validate a form using Ajax learn Ajax programming?

Enable the submit button in the input form. If the <valid/> element value is false, set the HTML of the validationMessage div element in Catalog ID field row to "Catalog Id is not Valid". Disable the submit button, and set the values of the other input fields. Next, run the Ajax application in JDeveloper 10.1.


1 Answers

Just for information you should not validate User code on the clientside. Always treat input from the client as evil

I changed the regex so that the min length (username.length < 4) of the Username is included

 function username_check() {
    var username = $('#warden_id').val();

    // if / else has been turned
    if (username.match(/^[a-zA-Z\s]{4,}$/)) {
        ...
    } else {
        $('#warden_id').css('border', '1px #CCC solid');
        $('#tick').hide();
    }
     ...

...
like image 116
winner_joiner Avatar answered Sep 18 '22 23:09

winner_joiner