Javascript Validation: Block Special Characters

Javascript validation: Block special characters

You have two approaches to this:

  1. check the "keypress" event. If the user presses a special character key, stop him right there
  2. check the "onblur" event: when the input element loses focus, validate its contents. If the value is invalid, display a discreet warning beside that input box.

I would suggest the second method because its less irritating. Remember to also check onpaste . If you use only keypress Then We Can Copy and paste special characters so use onpaste also to restrict Pasting special characters

Additionally, I will also suggest that you reconsider if you really want to prevent users from entering special characters. Because many people have $, #, @ and * in their passwords.

I presume that this might be in order to prevent SQL injection; if so: its better that you handle the checks server-side. Or better still, escape the values and store them in the database.

Javascript validation: Block special characters From input text box when click on submit

How can I restrict entering special characters in the text box (txt_edition)

Don't. Allow users to arrive at a valid value any way they like, you really only care that the value is valid when it's sent to the server, and even then you should validate there too.

by editing below code for validation? I want only numbers to be entered.

Just test when the form is submitted, passing a reference to the form in the call:

<form ... onsubmit="return validateForm(this)" >

and the function:

function validateForm(form) {
var value = form.txt_edition.value;

if (/\D/.test(value) || value == '') {
alert("Edition must be filled out and contain only numbers");
return false;
}
}

regex to restrict special characters except few

Instead of . that allows any character you can use a character set to restrict the allowed characters to a specific set:

^(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])(?=.*[+=!@#$%^&*])(?=\\S+$)[0-9A-Za-z+=!@#$%^&*]{14,}$

how to restrict special characters and ( /,*,+) only

replace this section:

if (keyCodeEntered == 45) {
// Allow only 1 minus sign ('-')...
if ((elementRef.value) && (elementRef.value.indexOf('-') >= 0))
return false;
{
else
return true;
}

with this:

 //         keys a-z,0-9               numpad keys 0-9            minus sign    backspace
if ( ( key >= 48 && key <= 90 ) || ( key >= 96 && key <= 105 ) || key == 109 || key==8)
{
//return true;
}
else
{
//return false
}
})

How to restrict input field from accepting special characters in Angular?

You could check the whole value to only match a selected character class and use for example .test()

^[\p{Alphabetic}\p{Mark}\p{Decimal_Number}\p{Connector_Punctuation}\p{Join_Control}]+$

See the regex matches.

Example

if (value === '' || !value.trim()) {
this.invalidNameFeedback = 'This field can not be blank.';
return false;
} else if (!/^[\p{Alphabetic}\p{Mark}\p{Decimal_Number}\p{Connector_Punctuation}\p{Join_Control}]+$/u.test(value)) {
this.invalidNameFeedback = 'This field can not contain special characters.';
return false;
}


Related Topics



Leave a reply



Submit