LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-21-2021, 08:07 AM   #1
despite
LQ Newbie
 
Registered: Feb 2021
Posts: 2

Rep: Reputation: Disabled
Alphanumeric username javascript


Hello,

I am trying to write some javascript code to validate a registration form. I use the match function of the string library, so for example
Quote:
"someusername".match(reg);
I know that there is plenty of resources on the Internet but unfortunately I have tried too many things and still validation does not work. Can someone please tell me how the regular expression should look like??
The regular expression is for the username which needs to be only alphanumeric.

Thank you.
 
Old 08-21-2021, 08:18 AM   #2
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,205
Blog Entries: 1

Rep: Reputation: 1565Reputation: 1565Reputation: 1565Reputation: 1565Reputation: 1565Reputation: 1565Reputation: 1565Reputation: 1565Reputation: 1565Reputation: 1565Reputation: 1565
Perhaps something like this:
Code:
var isalnum= /^[a-zA-Z0-9]+$/.test(username);
 
1 members found this post helpful.
Old 08-21-2021, 09:00 AM   #3
boughtonp
Senior Member
 
Registered: Feb 2007
Location: UK
Distribution: Debian
Posts: 1,618

Rep: Reputation: 1315Reputation: 1315Reputation: 1315Reputation: 1315Reputation: 1315Reputation: 1315Reputation: 1315Reputation: 1315Reputation: 1315Reputation: 1315

Alphanumeric-only usernames are probably not necessary, but everything you need to know should be described here:

https://developer.mozilla.org/en-US/docs/Learn/Forms/Form_validation

Also, a reminder that client-side validation is not a security measure - it doesn't avoid the need for checks on the server-side.

 
1 members found this post helpful.
Old 08-21-2021, 11:40 AM   #4
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 10,117

Rep: Reputation: 4687Reputation: 4687Reputation: 4687Reputation: 4687Reputation: 4687Reputation: 4687Reputation: 4687Reputation: 4687Reputation: 4687Reputation: 4687Reputation: 4687
You can use these sites to test your regex:

https://regexr.com/
https://regex101.com/

Anyway: "alphanumeric"? "^[A-Za-z0-9]+$"
 
1 members found this post helpful.
Old 08-23-2021, 11:11 AM   #5
despite
LQ Newbie
 
Registered: Feb 2021
Posts: 2

Original Poster
Rep: Reputation: Disabled
10 digit number regex

Hello,

Thank you all for your replies. It worked. One last thing. Could you please tell me the Javascript regular expression for a 10 digit number (telephone).

Thank you.
 
Old 08-23-2021, 11:46 AM   #6
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 16,854

Rep: Reputation: 5692Reputation: 5692Reputation: 5692Reputation: 5692Reputation: 5692Reputation: 5692Reputation: 5692Reputation: 5692Reputation: 5692Reputation: 5692Reputation: 5692
Code:
\d{10}
 
1 members found this post helpful.
Old 08-23-2021, 11:25 PM   #7
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,205
Blog Entries: 1

Rep: Reputation: 1565Reputation: 1565Reputation: 1565Reputation: 1565Reputation: 1565Reputation: 1565Reputation: 1565Reputation: 1565Reputation: 1565Reputation: 1565Reputation: 1565
(Note: my phone number is +36(1)555-1234)
 
1 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Auto increment an alphanumeric ID in C PMR Programming 3 05-22-2009 11:08 AM
cp will copy in any order, not alphanumeric DaveQB Linux - Software 3 01-10-2009 09:15 PM
AUTO_INCREMENT + alphanumeric in MySQL? ta0kira Programming 12 10-03-2008 12:46 PM
arithmetic on alphanumeric in PERL gaynut Programming 2 09-08-2008 09:09 AM
C fstat, st_uid and alphanumeric userID banan Programming 2 10-08-2006 06:17 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:22 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration