Jump to content

Adding in a regex to this script


acctman

Recommended Posts

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

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?

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)) {

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.