Javascript validation: Block special characters
You have two approaches to this:
- check the "keypress" event. If the user presses a special character key, stop him right there
- 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
Jquery Ajax Call to Controller Action Passing Array of Values
Allow Only One Checked Checkbox, Angular Best Practice.
Pass Node Js Variable to Ejs Template
How to Use Nestjs Logging Service
Reactjs: Expected Onclick Listener to Be a Function, Instead Got Type String
How to Show a Confirm Message Before Delete
How to Refresh Specific Div Using Javascript/Jquery With the Variables on It
How to Mock a Fake Database for When Unit Testing Against Knex
Possible to Detect If a User Has Multiple Tabs of Your Site Open
How to Push Array to Json Object in Angular
In React Why Does Calling a Function from Another Function in Our Code Not Work and Display
How to Load Content of Page Without Refreshing the Whole Page
Jquery - Get Id Dynamically from Generated Element
Uncaught (In Promise) Typeerror: Failed to Fetch and Cors Error
Onclick Event Not Working on Google Chrome
How to Detect iPad and iPad Os Version in iOS 13 and Up
Javascript to Download a File from Amazon S3 Bucket
Reverse Strings Without Changing the Order of Words in a Sentence