Web Analytics Made Easy -
StatCounter Regex Validation - CodingForum

Announcement

Collapse
No announcement yet.

Regex Validation

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Regex Validation

    How can I add

    Code:
    regex:"/^[a-0-9_-]$/",
    to the code below and use it to validate the username field?

    Code:
    pic1 = new Image(16, 16); 
    pic1.src = "loader.gif";
    $(document).ready(function(){
    $("#username").change(function() { 
    var usr = $("#username").val();
    if(usr.length >= 4)
    {
    $("#status").html('<img src="loader.gif" align="absmiddle">&nbsp;Checking availability...');
        $.ajax({  
        type: "POST",
        url: "check.php",  
        data: "username="+ usr,  
        success: function(msg){  
     
       $("#status").ajaxComplete(function(event, request, settings){ 
     if(msg == 'OK')
     { 
            $("#username").removeClass('object_error'); // if necessary
      $("#username").addClass("object_ok");
      $(this).html('&nbsp;<img src="tick.gif" align="absmiddle">');
     }  
     else  
     {  
      $("#username").removeClass('object_ok'); // if necessary
      $("#username").addClass("object_error");
      $(this).html(msg);
     }  
     
       });
     } 
     
      }); 
    }
    else
     {
     $("#status").html('<font color="red">The username should have at least <strong>4</strong> characters.</font>');
     $("#username").removeClass('object_ok'); // if necessary
     $("#username").addClass("object_error");
     }
    });
    });
    Thanks!

  • #2
    This is an invalid regex ... I suppose you meant something like
    Code:
    /^[a-z0-9_-]$/
    Then you can do this
    Code:
    var myRegex = /^[a-z0-9_-]$/;
    if(myRegex.test(usr)) {
       // this code will be executed if usr matches the regex
    }

    Comment


    • #3
      Where would I put it?
      Thanks!

      Comment


      • #4
        Maybe here ... but this depends on where you want to validate the field :-)
        Code:
        $("#username").change(function() { 
           var usr = $("#username").val();
           var myRegex = /^[a-z0-9_-]$/;
           if(usr.length >= 4 && myRegex.test(usr))
           ...
        });

        Comment


        • #5
          Now the script is failing. Before it would check if a username were available after I entered 4 or more characters. Now no matter what it displays the errors 'must be more than 4'.

          Code:
          pic1 = new Image(16, 16); 
          pic1.src = "loader.gif";
          $(document).ready(function(){
          $("#username").change(function() { 
             var usr = $("#username").val();
             var myRegex = /^[a-z0-9_-]$/;
             if(usr.length >= 4 && myRegex.test(usr))
           
          {
          $("#status").html('<img src="loader.gif" align="absmiddle">&nbsp;Checking availability...');
              $.ajax({  
              type: "POST",
           
              url: "check.php",  
              data: "username="+ usr,  
              success: function(msg){  
           
             $("#status").ajaxComplete(function(event, request, settings){ 
           if(msg == 'OK')
           { 
                  $("#username").removeClass('object_error'); // if necessary
            $("#username").addClass("object_ok");
            $(this).html('&nbsp;<img src="tick.gif" align="absmiddle">');
           }  
           else  
           {  
            $("#username").removeClass('object_ok'); // if necessary
            $("#username").addClass("object_error");
            $(this).html(msg);
           }  
           
             });
           } 
           
            }); 
          }
          else
           {
           $("#status").html('<font color="red">The username should have at least <strong>4</strong> characters.</font>');
           $("#username").removeClass('object_ok'); // if necessary
           $("#username").addClass("object_error");
           }
          });
          });
          Last edited by stevenmw; Sep 5, 2011, 06:57 AM.
          Thanks!

          Comment


          • #6
            It will fail if the username does not validate

            So if the username contains characters that don't match "a-z0-9_-" the "if" will return false and thus fail.

            Does the username contain characters other then lower case characters, 0-9, underscore or dash?

            Comment


            • #7
              No, even if the username matches the guidelines of the regex it fails.
              Thanks!

              Comment


              • #8
                Try this ... with the old code it will always fail, because it will only test for exactly ONE character out of the list
                Code:
                $("#username").change(function() { 
                   var usr = $("#username").val();
                   var myRegex = /^[a-z0-9_-]+$/;
                   if(usr.length >= 4 && myRegex.test(usr))
                Last edited by devnull69; Sep 5, 2011, 07:36 AM.

                Comment


                • #9
                  It works! What was the issue?
                  Thanks!

                  Comment


                  • #10
                    See the "+" inside the regex?

                    Comment

                    Working...
                    X
                    😀
                    🥰
                    🤢
                    😎
                    😡
                    👍
                    👎