cancel
Showing results for 
Search instead for 
Did you mean: 

javascript again

FIXED
Community Veteran
Posts: 3,274
Thanks: 339
Fixes: 12
Registered: 24-10-2013

javascript again

i have a telephone that is for entering a phone number.
the field can be left blank, but if it isn't blank then it must only contain numbers

what i have is

 

function validateForm() {

    var x10 = document.forms["addnewaddress"]["telno2"].value;
    var regex = new RegExp("^[0-9]+$");

    if ((!regex.test(x10)) && (x10 !==  null || x10 !== "")) {
       alert("Telephone number must contain numbers only");
       return false;
    } 

}



But that is forcing an entry to be made, so it is not accepting an empty field.
what is the correct syntax for this?

13 REPLIES
Community Veteran
Posts: 5,476
Thanks: 1,456
Fixes: 34
Registered: 16-10-2014

Re: javascript again

Do the test for empty first then if it is not empty then check its length, if its the correct size then validate it using the RegExp

Community Veteran
Posts: 3,274
Thanks: 339
Fixes: 12
Registered: 24-10-2013

Re: javascript again

the length could be various though.
as it could be a mobile, or a landline (which can be varying lengths).

and remember that an empty field is acceptable, so value 0 would be allowed?
Community Veteran
Posts: 5,476
Thanks: 1,456
Fixes: 34
Registered: 16-10-2014

Re: javascript again

It either has a number or it doesn't. If it has a number then that must meet the standard(s) you want to enforce:

i.e. 01234 123123 or

01234123123 or

+441234123123 or

the same for mobile, and any combination thereof.

0 is NOT a valid phone number so should not be allowed.

 

Community Veteran
Posts: 3,274
Thanks: 339
Fixes: 12
Registered: 24-10-2013

Re: javascript again

i should have said length is 0 not value.
the ++ number would not be allowed as a + is not a number.

so back to the original point, i can't work out the syntax to allow a blank entry or a validated number entry.

do i simply do mutiple separate checks? or can it be combined into one check.

Community Veteran
Posts: 3,274
Thanks: 339
Fixes: 12
Registered: 24-10-2013

Re: javascript again

so i now have it validating on 11 digits (assuming it's a mobile number for now).
but still need to add how to allow no entry in the field.

	if (/^\d{11}$/.test(x10)) {
	} else {
	    alert("Invalid mobile number; must be ten digits")
	    return false;
	}  
Community Veteran
Posts: 5,476
Thanks: 1,456
Fixes: 34
Registered: 16-10-2014

Re: javascript again


chenks76 wrote:

i should have said length is 0 not value.
1. the ++ number would not be allowed as a + is not a number.

so back to the original point, i can't work out the syntax to allow a blank entry or a validated number entry.

2. do i simply do mutiple separate checks? or can it be combined into one check.


1. + Is valid for a number and you can write your regex to make it optional

2. That depends on JavaScript really, but I think the answer is yes, some languages like VB will evaluate all conditions of the statement before coming to an answer, and I think JS is like C++, when the first fail condition is encountered then don't bother with the rest.

However, in your case I would have to do it over a couple of tests. If it has length then (assume) it's a phone number, if there is no length that's fine as that's allowed so move on. You may also want to move the RegEx portion of your code to it's own function so once you have a number you can then call:

isPhoneValid(telNo) ;

And you can use it in other places in your code.

Community Veteran
Posts: 5,476
Thanks: 1,456
Fixes: 34
Registered: 16-10-2014

Re: javascript again

This may be totally redundant for you but if your database allows you to store arrays then you can have an array field that will allow you to store any amount of numbers for an individual, either land line or mobile.

Contact VARCHAR(16)[] ;

I’m using 16 here as it’s a multiple of 8 which is always a good thing to do for text data.

Edit:- Added image of a SELECT from a table containing multiple array columns.

Screen Shot 2017-05-15 at 15.47.01.png

Community Veteran
Posts: 3,274
Thanks: 339
Fixes: 12
Registered: 24-10-2013

Re: javascript again

i'm specifically only allowing numbers, so + would not be valid for this purpose.

of course, for a mobile it's simply as it can only ever be 11 digits long.
but a UK geographic landling could be 10 or 11 digits (including the 0).

Community Veteran
Posts: 3,274
Thanks: 339
Fixes: 12
Registered: 24-10-2013

Re: javascript again

here's a jsfiddle of what i have just now

http://jsfiddle.net/yh0kc3p6/
Community Veteran
Posts: 3,274
Thanks: 339
Fixes: 12
Registered: 24-10-2013

Re: javascript again

Fix

sorted

	if ( (x10 == "") || (/^[0-9]{10,11}$/.test(x10)) ){
		} else {
		    alert("Invalid telephone number; must be 10 or 11 digits")
	    return false;
	}
Community Veteran
Posts: 5,476
Thanks: 1,456
Fixes: 34
Registered: 16-10-2014

Re: javascript again

You may want to say that land line numbers are 11 digits and mobiles 10, just to clarify.

Community Veteran
Posts: 3,274
Thanks: 339
Fixes: 12
Registered: 24-10-2013

Re: javascript again

landlines can be 10 or 11 digits.
Community Veteran
Posts: 5,476
Thanks: 1,456
Fixes: 34
Registered: 16-10-2014

Re: javascript again

OK, so maybe you could use this

placeholder="Enter a 10/11 digit number"

attribute in your input fields just as a pointer to the obvious.