javascript - How to use regex with can be empty? -


i'm doing easy web application practice skill.

i'm using regex validate phone input this,

var reg = /^[\s()+-]*([0-9][\s()+-]*){6,20}$/;  if(!reg.test($('#input-phone').val())){     alert('error'); } 

it works can't empty. don't know how modify able reject empty field.

check whether there value if use regex

var reg = /^[\s()+-]*([0-9][\s()+-]*){6,20}$/; var phone = $('#input-phone').val(); if (phone.length && !reg.test(phone)) {     alert('error'); } 

Comments

Popular posts from this blog

apache - PHP Soap issue while content length is larger -

asynchronous - Python asyncio task got bad yield -

javascript - Complete OpenIDConnect auth when requesting via Ajax -