javascript again
FIXED- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Plusnet Community
- :
- Forum
- :
- Other forums
- :
- Tech Help - Software/Hardware etc
- :
- javascript again
15-05-2017 1:34 PM - edited 15-05-2017 3:02 PM
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Report to Moderator
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?
Fixed! Go to the fix.

Re: javascript again
15-05-2017 2:52 PM - edited 15-05-2017 2:54 PM
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Report to Moderator
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
Re: javascript again
15-05-2017 2:58 PM
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Report to Moderator
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?

Re: javascript again
15-05-2017 3:07 PM
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Report to Moderator
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.
Re: javascript again
15-05-2017 3:13 PM - edited 15-05-2017 3:14 PM
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Report to Moderator
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.
Re: javascript again
15-05-2017 3:21 PM
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Report to Moderator
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; }

Re: javascript again
15-05-2017 3:32 PM - edited 15-05-2017 3:33 PM
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Report to Moderator
@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.

Re: javascript again
15-05-2017 3:45 PM - edited 15-05-2017 3:51 PM
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Report to Moderator
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.
Re: javascript again
15-05-2017 3:46 PM - edited 15-05-2017 3:46 PM
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Report to Moderator
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).
Re: javascript again
15-05-2017 5:45 PM
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Report to Moderator
16-05-2017 11:38 AM
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Report to Moderator
sorted
if ( (x10 == "") || (/^[0-9]{10,11}$/.test(x10)) ){ } else { alert("Invalid telephone number; must be 10 or 11 digits") return false; }

Re: javascript again
16-05-2017 11:52 AM
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Report to Moderator
You may want to say that land line numbers are 11 digits and mobiles 10, just to clarify.
Re: javascript again
16-05-2017 12:15 PM
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Report to Moderator

Re: javascript again
16-05-2017 1:51 PM
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Report to Moderator
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.
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page