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
Printable View
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
Which special characters? Or maybe it is easier to specify which characters should be allowed. RegExp could be useful for this job
i only want to allow 0-9 and a-z and ñ and Ñ,can you show me an example of what you suggested?tnx
Code:<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta http-equiv="Content-Style-Type" content="text/css">
<meta http-equiv="Content-Script-Type" content="text/javascript">
<script type="text/JavaScript">
function valid(f) {
!(/^[A-zÑñ0-9]*$/i).test(f.value)?f.value = f.value.replace(/[^A-zÑñ0-9]/ig,''):null;
}
</script>
</head>
<body><br>
<form id="myform" action="">
<input name="mytext" type="text" onkeyup="valid(this)" onblur="valid(this)">
</form>
</body>
</html>
wow it worked! tnx your a life saver man,one thing though when i try the ñ and Ñ it still get deleted...
In works for me. Well yes, I have no Spanish keyboard (thus I can not test onkeyup), but if I copy/paste the characters Ññ, they remain in the field. But it theory should work the same onkeyup as onblur.
What charset do you use? iso-8859-1 ?
Maybe use the unicode versions?I have no Spanish keyboard either, so I cannot check.Code:function valid(f) {
f.value = f.value.replace(/[^a-z\u00D1\u00F10-9]*/ig,'');
}
How to disallow characters ñÑ in the textfield?
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;
Read more about regular expressions in javaScript:Code:!(/^[A-zÑñ0-9-\s]*$/i).test(f.value)?f.value = f.value.replace(/[^A-zÑñ0-9-\s]/ig,''):null;
http://lawrence.ecorp.net/inet/samples/regexp-intro.php
Hello!
Works perfect! But, how can i change this to can write . : ! , characters?Quote:
function valid(f) {
f.value = f.value.replace(/[^A-zÑñ0-9-\s]/ig,'')
}
Can anyone help me?
This worked for me.Quote:
[A-zÑ.ñ0-9-\s\.\:\!]