Solution 1 :

Use ^[ws ,.]+$ for your validation.

You can check it online at

Problem :

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) {
    console.log("String is valid.");
    return true;
  else {
    console.log("String is invalid! Please re-enter");
    return false;

function reviewSubmittedForm() {
  var addressInput = document.querySelector(".address-input");
  if (runValidationForAddress(addressInput.value) === true) {
  else {
<input type="text" class="address-input" value="1600 Amphitheatre Parkway">
<input type="button" class="review-input" value="Submit">

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>


Comment posted by Falsehoods programmers believe about addresses

Very related:

Comment posted by Sandren Troy Milante

@VLAZ Do you have any suggestions or other improvements regarding your comment? May I know it? Anyways thank you for the info!

Comment posted by Toto

The space is useless in the character class as there is