Use ^[ws ,.]+$
for your validation.
You can check it online at https://regex101.com/r/q6LoSE/4.
Use ^[ws ,.]+$
for your validation.
You can check it online at https://regex101.com/r/q6LoSE/4.
I’m new at regex and I’m trying to create some validation process using regex. I’m checking if the given home address or location address is valid. I’m also validating other values using regex but they all work just fine, but this one only checks the first character entered. Try entering other special characters at any part of the value, the function will return true.
I’m only allowing letters, numbers, periods, commas, and whitespace.
Here’s the code [Excluded the working code and regex]:
document.querySelector(".review-input").addEventListener("click", reviewSubmittedForm);
function runValidationForAddress(stringToBeChecked) {
var regex = /^[a-zA-Z0-9\,\.s]/g;
if (regex.test(stringToBeChecked) === true) {
//Valid
console.log("String is valid.");
return true;
}
else {
//Invalid
console.log("String is invalid! Please re-enter");
return false;
}
}
function reviewSubmittedForm() {
var addressInput = document.querySelector(".address-input");
if (runValidationForAddress(addressInput.value) === true) {
console.log("Pass");
}
else {
console.log("Denied");
}
}
<input type="text" class="address-input" value="1600 Amphitheatre Parkway">
<input type="button" class="review-input" value="Submit">
<p>
After submitting, try to copy and paste this other location addresses:<br><br>
@1600 Amphitheater Parkway<br>
1600 @Amphitheatre $Parkway<br>
1600 #Amphitheatre Parkway<br>
</p>
Very related:
@VLAZ Do you have any suggestions or other improvements regarding your comment? May I know it? Anyways thank you for the info!
The space is useless in the character class as there is