acctman Posted May 4, 2009 Share Posted May 4, 2009 i'd like to add this /^([a-zA-Z0-9_]+)$/ regex to the username check <SCRIPT type="text/javascript"> $(document).ready(function(){ $("#username").change(function() { var usr = $("#username").val(); if(usr.length >= 4){ $("#status").html('<img src="/js/userchk/loader.gif" align="absmiddle"> Checking availability...'); $.ajax({ type: "POST", url: "/files/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(' <img src="/js/userchk/tick.gif" align="absmiddle">'); } else { $("#username").removeClass('object_ok'); // if necessary $("#username").addClass("object_error"); $(this).html(msg); } }); } }); } else { $("#status").html('<font color="red"><strong>Too short.</strong></font>'); $("#username").removeClass('object_ok'); $("#username").addClass("object_error"); }});}); </SCRIPT> Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted May 4, 2009 Share Posted May 4, 2009 Change if(usr.length >= 4){ To if (/^[\w]{4,}$/.test(usr)) { Quote Link to comment Share on other sites More sharing options...
acctman Posted May 4, 2009 Author Share Posted May 4, 2009 Change if(usr.length >= 4){ To if (/^[\w]{4,}$/.test(usr)) { can you explain what that regex does, that looks a lot different from mine... /^([a-zA-Z0-9_]+)$/; Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted May 4, 2009 Share Posted May 4, 2009 Sure. \w = [a-zA-Z0-9_] which stands for any word character. Same as yours. I just substituted it. You didn't need the parentheses, so I took them out. Then I have {4,} which matches the regex at least 4 times. Since you want to check if usr is 4 characters long and matches that expression, I simplified 2 statements into 1. Understand? Quote Link to comment Share on other sites More sharing options...
acctman Posted May 4, 2009 Author Share Posted May 4, 2009 Sure. \w = [a-zA-Z0-9_] which stands for any word character. Same as yours. I just substituted it. You didn't need the parentheses, so I took them out. Then I have {4,} which matches the regex at least 4 times. Since you want to check if usr is 4 characters long and matches that expression, I simplified 2 statements into 1. Understand? do i need to leave the word 'test' in there and if (/^[\w]{4,}$/.test(usr)) { Quote Link to comment Share on other sites More sharing options...
acctman Posted May 4, 2009 Author Share Posted May 4, 2009 by the way that regex worked perfectly... never knew you could shorten it like that. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.