I was wondering how I could block, not allow it, the ussage of the "@" on a textarea; and if they introduce it show them a warning.
Is the same if i want to block 8 digit numbers to?
Thanks!
You should prevent the use on the front end and the backend.
In php, you can easily do a check with strpos() to see if an @ sign exists. You could also use the same regex as below with preg_match()
Update: Apparently HTML5 textareas don't support the pattern attribute which seems strange to me. So this front end solution won't work for textareas, only inputs.
For HTML5, you can use the pattern attribute to specify the regex you are after. This is supported is nearly every browser now.
To unable to type the '@' char, you need to implement the code as follow:
<textarea class="area"> </textarea>
<script>
$('.area').keypress(function(event){
if ( event.which == 64 ) {
event.preventDefault();
}
});
</script>
The main key for this implementation is the event.preventDefault();
that you use to abandon the event that was trigger by key press.
And 64 is the ASCII code relate to '@'
If you want to limit the number of char typed on 8, it seems that the <input type="text">
better fit to you.
You can use code like so, which blocks many types of input a user could try to use to input an @
symbol:
var text = document.getElementById('txt');
text.onkeypress = function(e){
if(e.which === 64)
{
e.preventDefault();
return false;
}
};
text.onkeydown = function(e){
if(e.ctrlKey==true && (e.which == '118' || e.which == '86'))
{
e.preventDefault();
return false;
}
};
text.oncontextmenu = function(e){
e.preventDefault();
return false;
};
text.ondrop = function(){return false; };
The context menu portion is not compatible with earlier IE.
Note: This is only good as a precaution and I definitely suggest you validate this input on the back-end too, because obviously someone can just modify the front-end page so that they are allowed to submit whatever they want.
To block 8-digit numbers, you would have to have a regex function in an onsubmit
handler that checks that the field doesn't contain an 8-digit number.