Avatar of nigerman
nigerman

asked on 

I need a 10 digit telephone validated

I am very confused about something.

I have this js phone validation script:

      if (theForm.homePhone.value == "" || theForm.homePhone.value.length < 10){
            alert("The phone number is either blank or contains the wrong length. Phone must be 10 digits. Please make sure you included an area code.\n");
            theForm.homePhone.focus();
            return false;
      }

Then:

                           <tr>
                            <td>
                             <font color="#000000">Home Phone: </font><font color="#000000"><input name="homePhone" type="text" size="15" Value="" mask="(###) ###-####" onblur="return onBlurMask(this);" onfocus="return onFocusMask(this);" onkeydown="return doMask(this);"></font>
                             &nbsp;&nbsp;&nbsp;&nbsp;<font color="#000000">Emerg. Phone: </font><font color="#000000">
                                          <input name="emergencyPhone" type="text" size="16" Value="" mask="(###) ###-####" onblur="return onBlurMask(this);" onfocus="return onFocusMask(this);" onkeydown="return doMask(this);"></font>
                            </td>
                           </tr>




The idea is that you could not go beyond this box until you enter a 10 digit phone number.

How come someone is able to successfully submit data to the database with just 123456 as telephone number????

Is there anyway to prevent this from happening?
ASP

Avatar of undefined
Last Comment
nigerman
Avatar of abel
abel
Flag of Netherlands image

No, it is not possible to prevent that from happening. JavaScript is good for helping end users, but someone with JavaScript disabled, or someone with enough knowledge of HTTP (or who uses some tool) can submit any data to your web server. That means: you will always have to validate on the server too.

Usually, JavaScript is used for light validation (i.e., without the need to access a db) and the server is used for full validation.
Avatar of Muhammad Kashif
Use following code

 if (theForm.homePhone.value == ""){
            alert("The phone number is either blank or contains the wrong length. Phone must be 10 digits. Please make sure you included an area code.\n");
            theForm.homePhone.focus();
            return false;
      }
Avatar of abel
abel
Flag of Netherlands image

BTW: you do not show the code that actually does any validation. You show an if statement (that shows a messagebox when length is below 10, but length of 200 is still possible), but you don't show how it is used.

In addition, there's no event handler shown that does something on submit (meaning: the onclick of the input button). You do show to inputs that seem to have to take two phone numbers.
Avatar of nigerman
nigerman

ASKER

Thank you two very much for the prompt response.

abel. I guess you are right. I will attempt a server side validation  and let you guys know.
MuhammadKashif: The problem I have with your version is that now, you have made it even easier for the user to enter a one digit phone number.

I don't see much difference between your code and my code except that your version will allow less than 10 digits, no?
Avatar of nigerman
nigerman

ASKER

abel: I left those out intentionally:

function validate(theForm){
...
...

onsubmit="return validate(this);"
Avatar of abel
abel
Flag of Netherlands image

@MuhammadKashif: that code bit would check for an empty value only.

Another thing about the above code: you check for length, not for content. People can simply fill in a bunch of spaces and they're through. Unless the masking code prevents this (but you don't show that part).

It may also be that your masking code fills out the input text field with spaces, in which case you can check for lengths what you want, but the length will always be correct.

Supposing digits are the only allowed input and are checked correctly on keydown, the following function would work (the number 14 is taken from your mask).

function checkPhoneNumber(field)
{
    if (field.value.length < 14)
    {
         alert("wrong input");
         return false;
    }
}

Open in new window

Avatar of abel
abel
Flag of Netherlands image

> abel: I left those out intentionally:

I had to ask. Seen too many times where the calling code was missing or overlooked.

If the masking code fills out the input, you should check it with a regular expression instead. The regex could be:

with mask: \(\d{3}\) \d{3}-\d{4}
without mask: \d{10}
 
(which allows for leading and trailing zeroes, but not for spaces or anything not a digit, and the digits must be 10 in total)

Open in new window

Avatar of nigerman
nigerman

ASKER

abel:

This is good stuff from you. Thank you.

How do I use the regex completeley with the validation and masking code?

Can you give me the full code?

Thanks very, very much
Avatar of abel
abel
Flag of Netherlands image

You can use the following code. Then you put checkPhoneNumber in your onClick handler for the submit button as "return checkPhoneNumber(this);"

function checkPhoneNumber(field)
{
    var re = new RegExp("\\d{10}");   // note the extra backslash
    if (!field.value.match(re))
    {
         alert("wrong input");
         return false;
    }
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of abel
abel
Flag of Netherlands image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of nigerman
nigerman

ASKER

I am  trying to use the regex with mask but it isn't working for me.

    var re = new RegExp("\\d{3}- \\d{3}-\\d{4}");   // note the extra backslash
    if (!theForm.homePhone.value.match(re))
    {
		alert("The phone number is either blank or contains the wrong length. Phone must be 10 digits. Please make sure you included an area code.\n");
		theForm.homePhone.focus();
         return false;
    }

Open in new window

ASP
ASP

Active Server Pages (ASP) is Microsoft’s first server-side engine for dynamic web pages. ASP’s support of the Component Object Model (COM) enables it to access and use compiled libraries such as DLLs. It has been superseded by ASP.NET, but will be supported by Internet Information Services (IIS) through at least 2022.

82K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo