Special character validation
Simply add them to the character group. Of course, because both -
and /
are special characters in this context (/
ends a RegExp, -
expresses a range), you'll need to escape them with a preceding \
:
function validateAddress(){
var TCode = document.getElementById('address').value;
if( /[^a-zA-Z0-9\-\/]/.test( TCode ) ) {
alert('Input is not alphanumeric');
return false;
}
return true;
}
How to check for special characters when validating a form
You were catching every symbol.
Let's just simple only allowa-z
lowercase, A-Z
uppercase and or0-9
as @SterlingArcher said.
/[^a-zA-Z ]/g
Will allow only a-z and A-Z
/[^a-zA-Z0-9 ]/g
Will allow only a-z, A-Z, and 0-9
Letters and Numbers:
function ValidateActInsert() { var specialChars = /[^a-zA-Z0-9 ]/g; if (document.actorInsert.actInsert.value.match(specialChars)) { alert ("Only characters A-Z, a-z and 0-9 are allowed!") document.actorInsert.actInsert.focus(); return false; } return (true);}
<form name = "actorInsert" align = "center" action="actorInserting.php" onSubmit="return ValidateActInsert()"> Actor name:<br> <input type = "text" name = "actInsert" <br><br> <input type = "submit" value = "Insert"></form>
Validation for passwords with special characters
You can get through your requirement using a single regex alone. You may try the regex below:
^(?=\D\d)(?=[^A-Z]*[A-Z])(?=[^a-z]*[a-z])(?=[^-!@._*#%]*[-!@._*#%])[-A-Za-z0-9=!@._*#%]*$
Explanation of the above regex:
^, $
- Denotes the start and end of the line respectively.
(?=\D*\d)
- Represents a positive look-around which asserts for at least a digit.
(?=[^A-Z]*[A-Z])
- Represents a positive look-around which asserts for at least an upper case letter.
(?=[^a-z]*[a-z])
- Represents a positive look-around which asserts for at least a lower case letter.
(?=[^-!@._*#%]*[-!@._*#%])
- Represents a positive look-around which asserts for at least a symbol among the listed. You can add more symbols according to your requirement.
[-A-Za-z0-9=!@._*#%]*
- Matches zero or more among the listed characters. You can add more symbols accordingly.
You can find the demo of the above regex in here.
Sample implementation of the above regex in javascript:
const myRegexp = /^(?=[^\d\n]*\d)(?=[^A-Z\n]*[A-Z])(?=[^a-z\n]*[a-z])(?=[^-!@._*#%\n]*[-!@._*#%])[-A-Za-z0-9=!@._*#%]*$/gm; // Using \n for demo example. In real time no requirement of the same.const myString = `thisisSOSmepassword#T#!sIsS0om3%PasswordthisisSOSmepassword12thisissommepassword12#THISISSOMEPASSWORD12#thisisSOMEVALIDP@SSWord123`;// 1. doesn't contain a digit --> fail// 3. doesn't contain a symbol --> fail// 4. doesn't contain an Upper case letter --> fail// 5. doesn't contain a lowercase letter --> faillet match;// Taken the below variable to store the result. You can use if-else clause if you just want to check validity i.e. valid or invalid.let resultString = "";match = myRegexp.exec(myString);while (match != null) { resultString = resultString.concat(match[0] + "\n"); match = myRegexp.exec(myString);}console.log(resultString);
Related Topics
Javascript:Change the Function of the Browser'S Back Button
Sequlize Order by Only from Main Query
Nested Json: How to Add (Push) New Items to an Object
Regex - Get All Characters After Last Slash in Url
Cors - Response to Preflight Request Doesn't Pass Access Control Check
Enable Utf-8 Encoding for JavaScript
Regex to Match Words With Hyphens And/Or Apostrophes
Combining JavaScript and CSS into HTML File
How to Fix: Typeerror: Document.Queryselector(...) Is Null
Process a Dictionary and Return Each Entry in React
Angular 6 Store Data from Json Array Inside Json
Special Character Validation Using JavaScript
How to Check If Element Has Focused Child Using JavaScript
How to Append My Data in Nested Object
How to Detect the Mime Type of Data Url
Get the Value of Datepicker on Change in Jquery
Converting Async/Await Function to Normal Es5 Targetting Ie 11