6

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]+$/

1 Answers1

2

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();
    }
     ...

...
winner_joiner
  • 12,173
  • 4
  • 36
  • 61
  • Thanks for your suggestion, but some how i need to implement what i was saying. and this is where i need to match the inputs with the regex. 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(); } – Muhammad Mannan Masood Oct 01 '15 at 20:10
  • how can i add the regex match condition with and clause in if(response ==1).? – Muhammad Mannan Masood Oct 01 '15 at 20:11
  • It depends on what the response of the server is. What for data comes from the Server? You wrote **you want to validate the input**, but in the success function, the response ist the output from the server. – winner_joiner Oct 02 '15 at 03:04
  • @MannanMasood maybe you could edit/update your question, with the information, where you want to validate, and may be some example of input and output. – winner_joiner Oct 02 '15 at 03:07
  • @MannanMasood did may answer help? do you has some questions? – winner_joiner Oct 09 '15 at 12:48
  • sorry for late reply, yes it did helped me alot. Thank you soo much. – Muhammad Mannan Masood Dec 07 '17 at 12:53