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
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?

Link to comment
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?

 

do i need to leave the word 'test' in there and

if (/^[\w]{4,}$/.test(usr)) {

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.