asmith Posted November 24, 2007 Share Posted November 24, 2007 when a form says the username can contain only for example letters and numbers , how do it check the username ? for example if i use uername abc-123 , how can i find out i have used "-" in my username ? my english bad , i don't want to define all the "not-allowed" characters to find out, i want the backward thing, just check the allows characters, otherwise --> error . Quote Link to comment https://forums.phpfreaks.com/topic/78659-username/ Share on other sites More sharing options...
rajivgonsalves Posted November 24, 2007 Share Posted November 24, 2007 try this <? $username = "test"; if (preg_match("/^[a-zA-Z0-9\_\.]+$/",$username)) { echo "Valid username"; } else { echo "Not a valid username"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/78659-username/#findComment-398017 Share on other sites More sharing options...
asmith Posted November 24, 2007 Author Share Posted November 24, 2007 really thanks , could please tell me what /^[a-zA-Z0-9\_\.]+$/ means ? as i know preg match will ask , what are you looking for , and where are looking for , in what part, i only understood the a-zA-Z0-9. but what those / , ^ , \_\ , . +$ means ?? Quote Link to comment https://forums.phpfreaks.com/topic/78659-username/#findComment-398019 Share on other sites More sharing options...
BenInBlack Posted November 24, 2007 Share Posted November 24, 2007 I like to do it in Javascript html input field <input name=testfield value="hello-there" onblur="removeInvalidChars(this);"> Javascript code function removeInvalidChars(inputObj) { var regSpaces = new RegExp(/([-!@#$%])/g); if (regSpaces.test(inputObj.value)) { alert('Invalid Characters entered'); var objval = inputObj.value.replace(/([-!@#$%])/g,''); inputObj.value = objval; } } this code also removes the offending chars Quote Link to comment https://forums.phpfreaks.com/topic/78659-username/#findComment-398021 Share on other sites More sharing options...
rajivgonsalves Posted November 24, 2007 Share Posted November 24, 2007 it means aphabets ABC..Z or abc...z or 012..9 or "_" or "." is allowed Quote Link to comment https://forums.phpfreaks.com/topic/78659-username/#findComment-398022 Share on other sites More sharing options...
BenInBlack Posted November 24, 2007 Share Posted November 24, 2007 here is a handy link for learning Regex and testing it http://www.quanetic.com/regex.php Quote Link to comment https://forums.phpfreaks.com/topic/78659-username/#findComment-398024 Share on other sites More sharing options...
asmith Posted November 24, 2007 Author Share Posted November 24, 2007 here is a handy link for learning Regex and testing it http://www.quanetic.com/regex.php really useful , thanks . if i want to allow space , i should inert a \ \ ? like if (preg_match("/^[a-zA-Z0-9\_\ \.]+$/",$username)) Quote Link to comment https://forums.phpfreaks.com/topic/78659-username/#findComment-398028 Share on other sites More sharing options...
rajivgonsalves Posted November 24, 2007 Share Posted November 24, 2007 yes that would work Quote Link to comment https://forums.phpfreaks.com/topic/78659-username/#findComment-398029 Share on other sites More sharing options...
asmith Posted November 24, 2007 Author Share Posted November 24, 2007 thanks a bunch! Quote Link to comment https://forums.phpfreaks.com/topic/78659-username/#findComment-398033 Share on other sites More sharing options...
asmith Posted November 24, 2007 Author Share Posted November 24, 2007 oops ,another thing . how can i make the user be case-sensetive like password? Quote Link to comment https://forums.phpfreaks.com/topic/78659-username/#findComment-398036 Share on other sites More sharing options...
rajivgonsalves Posted November 24, 2007 Share Posted November 24, 2007 what do you mean case sensitive ? Quote Link to comment https://forums.phpfreaks.com/topic/78659-username/#findComment-398037 Share on other sites More sharing options...
asmith Posted November 24, 2007 Author Share Posted November 24, 2007 like these 2 usernames won't be equal : "abc" "aBc" actually i made a login page, but it doesn't care in the username field whether i type it uppercase , or lower case, but password is case-sensetive. Quote Link to comment https://forums.phpfreaks.com/topic/78659-username/#findComment-398044 Share on other sites More sharing options...
rajivgonsalves Posted November 24, 2007 Share Posted November 24, 2007 post your code maybe I could help Quote Link to comment https://forums.phpfreaks.com/topic/78659-username/#findComment-398045 Share on other sites More sharing options...
asmith Posted November 24, 2007 Author Share Posted November 24, 2007 it is too simple wen somebody fill the form user and password : script do this : insert into tableuser values ($_POST[username],$_POST[password]); then in login page, i have select * from tableuser where username='$_POST[username]' and password='$_POST[password]'"; . . . i have a test user name "john" , with password "qwerty", i can log in with this user and password , no problem but when i type the password "QWERTY" , it won't sign in , cos uppercase and lowercase matters to password. then again if i type the user "JOHN" and password "qwerty" , it works and sign in , cos uppercase and lowercase letters do not matter to user name , i want to know how can makes it matter to username ? like password, uppercase nad lowercase matters. example : john qwerty sign in JOHN qwerty won't sign in, Quote Link to comment https://forums.phpfreaks.com/topic/78659-username/#findComment-398046 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.