/    Sign up×
Community /Pin to ProfileBookmark

how to disable special characters in a textbox/textarea?

can anybody please show me how can i disable special characters in a textbox/textarea? im still not very skillful in javascript so i wanted to ask anyone to help me,tnx in advance

to post a comment
JavaScript

15 Comments(s)

Copy linkTweet thisAlerts:
@KorJul 02.2008 — [I]Which[/I] special characters? Or maybe it is easier to specify which characters should be allowed. RegExp could be useful for this job
Copy linkTweet thisAlerts:
@nebchill26authorJul 02.2008 — i only want to allow 0-9 and a-z and ñ and Ñ,can you show me an example of what you suggested?tnx
Copy linkTweet thisAlerts:
@KorJul 02.2008 — <i>
</i>&lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"&gt;
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Untitled Document&lt;/title&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"&gt;
&lt;meta http-equiv="Content-Style-Type" content="text/css"&gt;
&lt;meta http-equiv="Content-Script-Type" content="text/javascript"&gt;
&lt;script type="text/JavaScript"&gt;
function valid(f) {
!(/^[A-z&amp;#209;&amp;#241;0-9]*$/i).test(f.value)?f.value = f.value.replace(/[^A-z&amp;#209;&amp;#241;0-9]/ig,''):null;
}
&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;&lt;br&gt;
&lt;form id="myform" action=""&gt;
&lt;input name="mytext" type="text" onkeyup="valid(this)" onblur="valid(this)"&gt;
&lt;/form&gt;
&lt;/body&gt;
&lt;/html&gt;
Copy linkTweet thisAlerts:
@nebchill26authorJul 02.2008 — wow it worked! tnx your a life saver man,one thing though when i try the ñ and Ñ it still get deleted...
Copy linkTweet thisAlerts:
@KorJul 02.2008 — In works for me. Well yes, I have no Spanish keyboard (thus I can not test onkeyup), but if I copy/paste the characters &#209;&#241;, they remain in the field. But it theory should work the same onkeyup as onblur.

What charset do you use? iso-8859-1 ?
Copy linkTweet thisAlerts:
@Declan1991Jul 02.2008 — Maybe use the unicode versions?<i>
</i>function valid(f) {
f.value = f.value.replace(/[^a-zu00D1u00F10-9]*/ig,'');
}
I have no Spanish keyboard either, so I cannot check.
Copy linkTweet thisAlerts:
@jaghSep 05.2011 — How to disallow characters ñÑ in the textfield?
Copy linkTweet thisAlerts:
@KorSep 05.2011 — How to disallow characters ñÑ in the textfield?[/QUOTE]
As it is described above.
Copy linkTweet thisAlerts:
@jaghSep 07.2011 — Thanks, it works. But the space is not allowed to input. I need space to input two names in textbox/textarea (e.g. Maria Ana). How can i define it to this code:

!(/^[A-zÑñ0-9]*$/i).test(f.value)?f.value = f.value.replace(/[^A-zÑñ0-9]/ig,''):null;
Copy linkTweet thisAlerts:
@KorSep 07.2011 — <i>
</i>!(/^[A-z&amp;#209;&amp;#241;0-9[COLOR="Blue"]-s[/COLOR]]*$/i).test(f.value)?f.value = f.value.replace(/[^A-z&amp;#209;&amp;#241;0-9[COLOR="Blue"]-s[/COLOR]]/ig,''):null;

Read more about regular expressions in javaScript:

http://lawrence.ecorp.net/inet/samples/regexp-intro.php
Copy linkTweet thisAlerts:
@DexTeR1911Feb 10.2013 — Hello!


function valid(f) {

f.value = f.value.replace(/[^A-z&#209;&#241;0-9-s]/ig,'')

}
[/QUOTE]


Works perfect! But, how can i change this to can write . : ! , characters?

Can anyone help me?
Copy linkTweet thisAlerts:
@LukeismeMar 22.2013 — [A-z&#209;&#46;&#241;0-9-s.:!][/QUOTE]
This worked for me.
Copy linkTweet thisAlerts:
@jwlambJul 01.2013 — Ok, I've tweaked the code a bit and got it close to where I want it, but some characters are still popping up. How do I limit it to just A-z Ñ ñ - , . ' and spacebar

I don't want numbers or any weird special characters, this is for customers to order uniform nametapes on my website, so just what would be in someone's name including Spanish lettering and hyphenated names. Also if they wanted last, first or Last, F. And anything like O'whatever should also be allowed. When I type it in as:

!(/^[A-zÑñs-,.']*$/i).test(f.value)?f.value = f.value.replace(/[^A-zÑñs-,.']/ig,''):null;

it is still allowing the ^ _ [ ] and any other character using alt#

MANY THANKS!
Copy linkTweet thisAlerts:
@cartoon_bagJul 01.2013 — You could set up a bunch of regular expression that checked for link code and abort the form. E.g. a whole string of things like.

cool things
×

Success!

Help @nebchill26 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...