1

I need a validation for address that can have Numbers, alphabets, space in two words should be able .. but no space in the beginning of the address. The only symbol should be #
EX: #55 Bernahrd Gym

Address:<input type="text" required="required" name="baguio_add"><br>

chaka Doll
  • 11
  • 1

2 Answers2

1

You can use regular expressions for this: /[a-z0-9\s\#]+/i

Instead of testing for spaces at the start, just use trim (or ltrim).


Edit: You changed your tag to Javascript, you can still use regular expressions for this using the regex above.

cjhill
  • 1,004
  • 2
  • 9
  • 31
0

You can use this regex if you want only one #, and any number of alpha numeric plus spaces.

/#?[a-zA-Z\s\d]+/

If it always starts with # then:

/^(#?)[a-zA-Z\s\d]+$/

Here is how you use it:

HTML:

<input name="address" id="address" type="text" />

Javascript:

document.getElementById('address').onblur = function (e) {
    console.log(this.value);
    var isValid = /^(#?)[a-zA-Z\s\d]+$/.exec(this.value);
    if (isValid){
      // do any action on valid
      this.className = 'valid';
    } else {
        this.className = 'invalid';
    }
}

Here is a working example in jsfiddle: https://jsfiddle.net/uua6pp1q/

Arathi Sreekumar
  • 2,544
  • 1
  • 17
  • 28