473,327 Members | 2,071 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,327 software developers and data experts.

Stop user from inputing special chars. Only a-z 0-9 allowed.

Markus
6,050 Expert 4TB
How would i go about checking for things such as *,&,%,£ etc.etc. in a user inputted string.

The code im working on is a sign up script, which i'm trying to make by myself and not use someone elses code, because that just wouldn't benefit my learning of JavaScript.

If you do happen to know how i might do what i want, might you also explain it?

Thanks a bunch.
Nov 10 '07 #1
4 2742
mrhoo
428 256MB
One way to filter users' text input is to use regular expressions.


Expand|Select|Wrap|Line Numbers
  1. function validText(str){
  2.     return str.replace(/[^a-zA-Z0-9]/g,'');
  3. }
This will remove any characters that are not in the ranges listed in the expression.

You may want to expand the pattern for names, to include

Carroll O'Conner and Edward R. Murrow and Michael Wilkenson-Barre
Nov 10 '07 #2
Markus
6,050 Expert 4TB
One way to filter users' text input is to use regular expressions.


Expand|Select|Wrap|Line Numbers
  1. function validText(str){
  2.     return str.replace(/[^a-zA-Z0-9]/g,'');
  3. }
This will remove any characters that are not in the ranges listed in the expression.

You may want to expand the pattern for names, to include

Carroll O'Conner and Edward R. Murrow and Michael Wilkenson-Barre
So if i were to expand that to include underscores (_) and hyphons (-) i would do this:

Expand|Select|Wrap|Line Numbers
  1. (/[^a-ZA-Z0-9_-]g, '');
  2.  
?

And by the way, it's not that i want to replace the characters, i just want to check for them and alert the user as to the problem.

Thanks for the explanation, aswell!
Nov 10 '07 #3
mrhoo
428 256MB
Since the hyphen is used to delimit the range of characters you must escape it by preceding it with a backslash:
/[^\w\-]/

Here the \w is a shortcut often used, it is the pattern described by
/[^a-zA-Z0-9_]/

And the leading caret [^ means match anything NOT in the range described.
Nov 10 '07 #4
Markus
6,050 Expert 4TB
Since the hyphen is used to delimit the range of characters you must escape it by preceding it with a backslash:
/[^\w\-]/

Here the \w is a shortcut often used, it is the pattern described by
/[^a-zA-Z0-9_]/

And the leading caret [^ means match anything NOT in the range described.
Thanks a bunch :)

- mark
Nov 10 '07 #5

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

Similar topics

14
by: tertius | last post by:
Is there a better way to append certain chars in a string with a backslash that the example below? chr = "#$%^&_{}" # special chars to look out for str = "123 45^ & 00 0_" # string to...
6
by: Jonas Meurer | last post by:
hello, my script selects a comment saved as VARCHAR in MySQL and displays it inside an html page. the problem is, that the comment contains several special characters, as mysterious utf-8...
2
by: Mike Bennett | last post by:
Does the .NET framework (using VB.NET) support the ability to programmatically STOP a device prior to its removal from the system? I have need to move data from a computer on one network to a...
6
by: Mark | last post by:
I am designing a game for a forum. When the user has finished playing I need to save their data to a cookie then navigate to a page which holds their score data (I can't have both sets of data on...
4
by: dutch disCo | last post by:
Hi, I'm trying to use GET method to pass variables through my script files. Unfortunately, when a special char (&, ?, +) is being posted the variable gets trimmed in a very strange way. I'm...
3
by: VMI | last post by:
When I execute a ReadLine from an ascii file with special chars (ie. the 'Ñ' in "NUÑEZ PEREZ"), it automatically deletes this character. So "NUÑEZ PEREZ" becomes "NUEZ PEREZ". How can this be...
6
by: Patrick Olurotimi Ige | last post by:
I would like to capture when a user has already selected an option from a multi select list / or Textbox So for example if a user selects a value 3 using a dropdown list or by inputing a value it...
5
by: Alex | last post by:
Hi all - Is there a standard way to handle special chars in strings in dotnet? I'm using csharp and having two seperate problems... 1. Passing in args... I need to pass in an arg that...
33
by: JamesB | last post by:
I am writing a service that monitors when a particular app is started. Works, but I need to get the user who is currently logged in, and of course Environment.UserName returns the service logon...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.