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> Link to comment https://forums.phpfreaks.com/topic/156847-adding-in-a-regex-to-this-script/ 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)) { Link to comment https://forums.phpfreaks.com/topic/156847-adding-in-a-regex-to-this-script/#findComment-826258 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_]+)$/; Link to comment https://forums.phpfreaks.com/topic/156847-adding-in-a-regex-to-this-script/#findComment-826261 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? Link to comment https://forums.phpfreaks.com/topic/156847-adding-in-a-regex-to-this-script/#findComment-826263 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)) { Link to comment https://forums.phpfreaks.com/topic/156847-adding-in-a-regex-to-this-script/#findComment-826309 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. Link to comment https://forums.phpfreaks.com/topic/156847-adding-in-a-regex-to-this-script/#findComment-826330 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.