473,400 Members | 2,163 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,400 software developers and data experts.

Validating Username

i have used the following code to validate the username it is working fine
Expand|Select|Wrap|Line Numbers
  1. if( $username == "" ||  !preg_match("/^[a-z0-9]+(?:_[a-z0-9]+)?$/i", $username) )
  2. {
  3.    $error.="User name cannot be blank or has special characters";
  4. }     
  5.  
it does not accept UNDERSCORE at the beginning or end however while i was testing with different special characters except for # the validation works fine for all other special characters.

for example if i enter the user name as = abc#123

in this case # sign and what comes after # sign is being ignored. so in this case the username is being read as abc ONLY and not abc#123

this is very strange, how can i still validate # sign and tell the user that # sign is not a valid username like i have been doing with any other special characters like = !@$...........

please advice.

thanks.
May 26 '08 #1
24 2951
Markus
6,050 Expert 4TB
Cannot understand your problem?

What do you want your regex to accept and not accept?

Maybe we could help you create a new one.
May 26 '08 #2
hsriat
1,654 Expert 1GB
in this case # sign and what comes after # sign is being ignored. so in this case the username is being read as abc ONLY and not abc#123
Its not a problem with regexp.

It seems like you are submitting your username with ajax and not doing encodeURIComponent() to it before submitting.
May 26 '08 #3
Atli
5,058 Expert 4TB
Also, if you are submitting your usernames as GET variables, via the URL, the # character and anything that follows it will be parsed as an anchor name by the browser.

If that is the case, try the urlencode and urldecode functions.
May 26 '08 #4
hsriat
1,654 Expert 1GB
Also, if you are submitting your usernames as GET variables, via the URL, the # character and anything that follows it will be parsed as an anchor name by the browser.

If that is the case, try the urlencode and urldecode functions.
But I thought the browser in itself always does urlencoding of the from elements before sending them by GET method (except when using Ajax). Don't you think so?

Try this example...
[HTML]<html>
<form action="page_doesnt_exist.htm">
<input name="param" value="">
<input type="submit" value="Check what's the url">
</form>
</html>[/HTML]
Just type in a value containing # in the field and click button to see what's the url. Isn't it already urlencoded?

I typed abc#xyz and got page_doesnt_exist.htm?param=abc%23xyz in the address bar when clicked on submit.
# got encoded to %23
May 26 '08 #5
Atli
5,058 Expert 4TB
But I thought the browser in itself always does urlencoding of the from elements before sending them by GET method (except when using Ajax). Don't you think so?
True. If you submit an actual <form> the browser should take care of this for you.

I was thinking more along the lines of putting them manually in the URL string.
Like if you had made a list of users and wanted each name to link to another script, passing along the username in the URL.

Could have chosen my words more carefully in my previous post. Sorry about that.
May 26 '08 #6
Markus
6,050 Expert 4TB
Where did you get the idea that this is to do with ajax or URLs?
May 26 '08 #7
Markus
6,050 Expert 4TB
Oh, because # marks a placement in the page and what comes after that is looked for in a div id.

I see.

But why submit it through GET?

Makes more sense for POST, right?
May 26 '08 #8
hsriat
1,654 Expert 1GB
Where did you get the idea that this is to do with ajax or URLs?
Well, any variable (username in this case) can be posted by 4 ways (according to my knowledge).

1. Simple GET - # won't give any trouble as its already urlencoded by browser.
2. Simple POST - same here...
3. Ajax GET - # could cause a problem, as # in url is for anchors
4. Ajax Post - not sure about this one..

So I thought it could be case 3.
May 26 '08 #9
ronverdonk
4,258 Expert 4TB
......because # marks a placement in the page and what comes after that is looked for in a div id.....
Sorry to disagree slightly here.
A destination anchor is not just, or only, a div. The destination anchors in HTML documents may be specified either by the A element (naming it with the name attribute), or by any other element (naming with the id attribute).

Ronald
May 26 '08 #10
hsriat
1,654 Expert 1GB
True. If you submit an actual <form> the browser should take care of this for you.

I was thinking more along the lines of putting them manually in the URL string.
Like if you had made a list of users and wanted each name to link to another script, passing along the username in the URL.

Could have chosen my words more carefully in my previous post. Sorry about that.
Nothing to sorry about dude. :)

Yeah, in the case you said, one should do urlencode in PHP before sending the HTML to the browser.

Regards
May 27 '08 #11
my question is about validation using php. i am validating a username which a user would enter and clicks on a image to find

if that username is available. example if a user enters abc#123 php file is reading this value as abc ONLY which i do not

want instead the php file should read as abc#123. follow is the sequence of pages. please advice the solution.

first page = register.php here a user enters a username and clicks on an image to find out if the username is available or

not. using a javascript function of onclick i am reading the value entered in the form in javascript as
Expand|Select|Wrap|Line Numbers
  1. var useri = document.registrationform.username
  2. var valueofuseri = document.registrationform.username.value
  3.  
  4. var recui = /^\s{1,}$/g;
  5.  
  6. if ((useri.value==null) || (useri.value=="") || (useri.length=="") || (useri.value.search(recui))> -1)
  7. {
  8. alert("Please Enter a User Name")
  9. return false
  10. }
  11.  
  12. window.open("checkusernamei.php?theusernameis="+valueofuseri, "titleforavailabilityi", "width=680,  height=275, status=1, 
  13.  
  14. scrollbars=1, resizeable=yes");
  15.  
second page = checkusernamei.php = this file uses GET to read what was entered in the form.
Expand|Select|Wrap|Line Numbers
  1. $username = $_GET["theusernameis"];
  2.  
  3. if( $username == "" ||  !preg_match("/^[a-z0-9]+(?:_[a-z0-9]+)?$/i", $username) )
  4. {
  5. echo "username is blank or has special characters";
  6. }
  7.  
the # sign is being ignored only if the image is clicked in order to check the username, if the user enters abc#123 and

clicks the submit button without clicking on the checkuser image button then my php validation for username shows an error

message.

Expand|Select|Wrap|Line Numbers
  1. if( $username == "" ||  !preg_match("/^[a-z0-9]+(?:_[a-z0-9]+)?$/i", $username) )
  2. { echo "display error message for username"; }
  3.  
now the problem is with clicking the image only and passing using GET method how can i fix this problem.

please advice.

thanks.
May 27 '08 #12
hsriat
1,654 Expert 1GB
Expand|Select|Wrap|Line Numbers
  1. var valueofuseri=encodeURLComponent(document.registrationform.username.value);
Dude, this is what I suggested you in the other thread on same problem.
I guess you could not locate that thread in the long list of threads.

PS: Use [CODE] Tags.
May 27 '08 #13
Atli
5,058 Expert 4TB
Please do not double post your questions.
That only serves to cause confusion, waste the time of our fellow members and make us moderators mad!

And use [code] tags when posting your code examples.

Seeing as you have over 40 posts already, I expect you to know this by now.
Take a look at our Posting Guidelines if you haven't already.
I will not be happy if I need to repeat this warning!

I have merged the other thread into this one.

Moderator
May 27 '08 #14
pbmods
5,821 Expert 4TB
Heya, Runway.

What is the value of $username when the User inputs 'abc#123'?
May 27 '08 #15
dlite922
1,584 Expert 1GB
Heya, Runway.

What is the value of $username when the User inputs 'abc#123'?
HEY YOUR BACK!!

*goes to cafe/lounce to see announcement*

I think he said the value is only "abc", the rest is truncated.

he needs url_encoding i believe, as every body here posted (some more than once)

PS: glad to see you back,

Dan
May 27 '08 #16
my question is about validation using php. i am validating a username which a user would

enter and clicks on a image to find if that username is available. example if a user enters

abc#123 php file is reading this value as abc ONLY which i do not want instead the php file

should read as abc#123. following is the sequence of pages. please advice the solution.

first page = register.php here a user enters a username and clicks on an image to find out

if the username is available or not. using a javascript function of onclick i am reading the

value entered in the form in javascript as
=============================================
var useri = document.registrationform.username
var valueofuseri = document.registrationform.username.value

var recui = /^\s{1,}$/g;

if ((useri.value==null) || (useri.value=="") || (useri.length=="") ||

(useri.value.search(recui))> -1)
{
alert("Please Enter a User Name")
return false
}

window.open("checkusernamei.php?theusernameis="+va lueofuseri, "titleforavailabilityi",

"width=680, height=275, status=1, scrollbars=1, resizeable=yes");

============================================

i have used a alert message in javascript to display the value, javascript is able to

capture all the characters entered which is abc#123

second page = checkusernamei.php = this file uses GET to read what was entered in the form.
============================================
$username = $_GET["theusernameis"];

if( $username == "" || !preg_match("/^[a-z0-9]+(?:_[a-z0-9]+)?$/i", $username) )
{
echo "username is blank or has special characters";
}
============================================
the # sign is being ignored only if the image is clicked in order to check the username, if

the user enters abc#123 and clicks the submit button without clicking on the checkuser image

button then my php validation for username shows an error message.

================================================== ============
if( $username == "" || !preg_match("/^[a-z0-9]+(?:_[a-z0-9]+)?$/i", $username) )
{ echo "display error message for username"; }
================================================== ============
now the problem is with clicking the image only and passing the value to checkusernamei.php

using GET method
i have also used an echo statement in checkusernamei.php as
echo "value of username is ". $username; = this displays abc and not abc#123

how can i fix this problem wherein checkusernamei.php will be able to read abc#123. also in

this checkusernamei.php file i have a select query which will read if the username already

exists in the table. presently as checkusernamei.php is reading abc ONLY the select query is

also passing abc and not abc#123

$select = "Select username from table where username = '$username'";

please advice.

thanks.
May 29 '08 #17
hsriat
1,654 Expert 1GB
Dear runway27,

When # goes in the URL, the text after the # is considered as anchor by the browser. When you are opening a new window with this code:
Expand|Select|Wrap|Line Numbers
  1. window.open("checkusernamei.php?theusernameis="+valueofuseri, "titleforavailabilityi", "width=680, height=275, status=1, scrollbars=1, resizeable=yes");
The URL is "checkusernamei.php?theusernameis="+valueofuse ri

And when your user name is abc#123, your URL becomes:
checkusernamei.php?theusernameis=abc#123

So what happens is, #123 is considered as an anchor bookmark(see point 2 here) in the page.

With this, the value of your $_GET['theusernameis'] remains abc only.

To escape this, you should use encodeURIComponent in JavaScript.
So just change the second like of your JavaScript as
Expand|Select|Wrap|Line Numbers
  1. var valueofuseri = encodeURIComponent(document.registrationform.username.value)
If you want to encode it in PHP, after you click on the page and its opened in PHP, then that would be too late. As by that time your browser would have already considered text after # as anchor.

I apologize that I answered you twice on the same question but could not make you understand why is it so.

Hope you will get this this time.

Regards
May 30 '08 #18
Markus
6,050 Expert 4TB
Deja-vu.

Hasn't this been asked like 3 times (and answered)?
May 30 '08 #19
hsriat
1,654 Expert 1GB
Deja-vu.

Hasn't this been asked like 3 times (and answered)?
I too think so... ;)
May 30 '08 #20
Atli
5,058 Expert 4TB
Deja-vu.

Hasn't this been asked like 3 times (and answered)?
Indeed. This will be the second time I merge a duplicate thread with the original thread.

runway27, you have been warned twice before about double-posting and using [code] tags, and you just keep ignoring us.

This behavior is not acceptable and has just earned you a 1 week ban.
I suggest you take that time to familiarize yourself with the Posting Guidelines.

The next time you ignore the guidelines, the ban will be permanent.

Moderator
May 30 '08 #21
hsriat
1,654 Expert 1GB
This behavior is not acceptable and has just earned you a 1 week ban.
aww.. this is gonna be harsh. But rules are rules.


I didn't get one thing though... he never replied in the already started thread. He got many replies, but he didn't give a damn.

What did he think about us?.....
May 30 '08 #22
Markus
6,050 Expert 4TB
Sorry to disagree slightly here.
A destination anchor is not just, or only, a div. The destination anchors in HTML documents may be specified either by the A element (naming it with the name attribute), or by any other element (naming with the id attribute).

Ronald
I wasn't being specific to only divs.
May 30 '08 #23
Markus
6,050 Expert 4TB
aww.. this is gonna be harsh. But rules are rules.


I didn't get one thing though... he never replied in the already started thread. He got many replies, but he didn't give a damn.

What did he think about us?.....
I just don't understand some people..

Could it've been a bot?
May 30 '08 #24
hsriat
1,654 Expert 1GB
I just don't understand some people..

Could it've been a bot?
LOL!... and that bot has just got to ask a technical question in bytes?... not a good utility bot.
May 30 '08 #25

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Ralph Freshour | last post by:
I'm looking in my PHP book but still figure out the best way to validate that certain characters are contained in a string - for example, when a user enters a username, I want to limit the...
0
by: Ramnadh Nalluri | last post by:
Hi, I am creating DirectoryEntry in the C#.Net by taking the constructor DirectoryEntry("LDAP://<Domain>",<UserName>,<Pwd>,AuthenticationType.Sec ure) and creating the DirectoryEntry. But i...
4
by: Patrick.O.Ige | last post by:
I have a code below.(It validates against a SQL DB(login page).thats is giving me an error! When i try to use :- Session = dr.ToString(); To catch the username so as to redirect the user logged in...
3
by: Patrick.O.Ige | last post by:
I have a problem with the code below:- When i use a username for example dog for the first time it works but later when i use cat for example it keeps showing Hello:- Dog.. It keeps DOG in the...
2
by: Patrick.O.Ige | last post by:
Is it possible to execute a query or do some validation before loading a page.. I have a survey page and i want to validate users before loading the page. I have done it in the page_load. But the...
0
by: Steve | last post by:
I have created a tableadaptor in VWD called "MemberUsernameTableAdaptor" with a "GetUsername()" method. The method requires the @username parameter and will then try matching it up in the username...
4
by: ryan | last post by:
i am storing a usernames and passwords in a table called Users. I present a login form to the user when my application starts up (VB.NET, .NET CF, Windows Mobile 5) The user chooses a username...
5
by: Sudhakar | last post by:
i have used the following code to validate the username it is working fine ============================================= if( $username == "" || !preg_match("/^+(?:_+)?$/i", $username) ) {...
6
by: phpmagesh | last post by:
Hi I am creating login page with validation. i have index.php page with login box, for validating this login detail i m redirecting to login_validate.php. Login_validate.php file code: ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.