473,585 Members | 2,501 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Validating Username

54 New Member
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 2972
Markus
6,050 Recognized Expert Expert
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 Recognized Expert Top Contributor
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 encodeURICompon ent() to it before submitting.
May 26 '08 #3
Atli
5,058 Recognized Expert Expert
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 Recognized Expert Top Contributor
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_do esnt_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_exi st.htm?param=ab c%23xyz in the address bar when clicked on submit.
# got encoded to %23
May 26 '08 #5
Atli
5,058 Recognized Expert Expert
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 Recognized Expert Expert
Where did you get the idea that this is to do with ajax or URLs?
May 26 '08 #7
Markus
6,050 Recognized Expert Expert
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 Recognized Expert Top Contributor
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 Recognized Expert Specialist
......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

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

Similar topics

3
3223
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 characters to a..z, A..Z,0..9 only. What's the best way to check for this? Thanks...
0
1273
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 am connecting through VPN (Virtual Private Network) when i am connecting through VPN i am taking the Username,Domain and Pwd and creating a...
4
1999
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 to another page....PRINTING Hello:- John .. for example It says ERROR:- System.IndexOutOfRangeException: username.Text What does this mean and...
3
1245
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 session for long. But i want to get the USERNAME from the datbase and display it so it catches only the username from the DB only.. I think 'm...
2
1349
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 problem i have is that if the user gets the URL and directly paste it to the browser the USER still sees the page. Is there a way how to check the...
0
1161
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 field of a table. As the usernames are all unique, if the username exists a 1 row dataset will be returned, if not the dataset will be empty. ...
4
2591
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 from a combo box that queries the Users table. Then they type in a password string. I then use the following code to validate the string when the...
5
1583
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) ) { $error.="User name cannot be blank or has special characters"; } =============================================
6
1344
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: <?php
0
7908
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7836
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8336
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7950
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8212
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6606
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5389
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
1
1447
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1175
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.