Solved

How do I perform a phone number look up before form is submitted?

Posted on 2004-04-29
22
215 Views
Last Modified: 2013-12-24

I have a form that ask for the phone number   area core - prefix - number

<input name="AHPhone" type="text"  size="3" maxlength="3" onKeyup="autotab(this, document.form.HPhone1)">
   -                           <input name="HPhone1" type="text"  size="3" maxlength="3" onKeyup="autotab(this, document.form.HPhone2)">
                          <input name="HPhone2" type="text"  size="4" maxlength="4" onKeyup="autotab(this, document.form.AWPhone)">

I also have a database containing a table with all the US/Canada area codes and prefixes. I want to validate the phone number input of the user BEFORE submiting the form. I need to validate that the area code exist on the area_code field from the table and the prefix also exist. Also that the prefix correspond to that area code.

I'm using cf.  Is it posssible to include a <cfquery> within a javascript function?
0
Comment
Question by:gaby22
  • 10
  • 6
  • 5
  • +1
22 Comments
 
LVL 35

Expert Comment

by:mrichmon
ID: 10950998
No You can't include a cfquery within javascript - javascript runs client side and cold fusion runs server side.

You can however validate after submitting the form - or by using a javascript iFrame to perform a background submit of just the phone number to validate it.

But in either case it will be submitted before it can be validated.
0
 

Author Comment

by:gaby22
ID: 10951918
How do I perform a javascript iFrame? Will it check in the BD if the phone number is OK?
0
 
LVL 35

Expert Comment

by:mrichmon
ID: 10952041
Look at this thread where I explained how to do this:

http:Q_20936125.html


Basically what happens is that the iFrame submits to a cold fusion page in the background and does whatever checks you want against the database.  Then it sends back to the iFrame the results and the iFrame puts an error message on the page or even a success message...
0
 

Author Comment

by:gaby22
ID: 10953607
Are you sure there is not any other simpler way to do this? I'm lost. Could this be an option:  making a query at the begining of the page, storing the the area code values in a list and comparing the user's  imput against the list values?  
0
 
LVL 35

Expert Comment

by:mrichmon
ID: 10953755
You could, but since you would have to store all the DB contents (i.e. all the posibilities) this could be huge.

I would recommend as teh simplest solution

1) User enters phone number on form
2) User submits form
3) In cold fusion you compare by doing queries to make sure it is correct - if so continue - otherwise send them back to the page with an error message.

Using this method you can re-fill in the form so that the only difference is that the error is displayed.

This is the easiest way to do this BUT it does require submitting the form.
0
 
LVL 17

Expert Comment

by:Tacobell777
ID: 10954326
The cfinput tag might be of help
<cfinput name="telephone" type="text" required="no" validate="telephone">
remember it needs to be in a cfform tag..
0
 
LVL 17

Expert Comment

by:Tacobell777
ID: 10954336
Yes cfform is bad for expert programmers, but not for other people out there that want it done the easy way.
0
 
LVL 35

Expert Comment

by:mrichmon
ID: 10954351
but he doesn't want just is it a valid format - gaby wants to validate it against a database of known area codes - this cannot be done with cfform.
0
 
LVL 17

Expert Comment

by:Tacobell777
ID: 10954416
Why does this check need to be done on the client side? It not foul proof doing it this way, do it on the server-side there is nothing wrong with doing it on the server-side and you save yourself a lot of time and hassles working with JS and iFrames.
0
 
LVL 17

Expert Comment

by:Tacobell777
ID: 10954512
<cfset result = reFindNocase("\d*\s*Results Found", string, 1, "true")>

<cfdump var="#result#">

Use mid to retrieve the string found
0
 
LVL 17

Expert Comment

by:Tacobell777
ID: 10954513
oops wrong thread...
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 35

Expert Comment

by:mrichmon
ID: 10954518
yep that was my recommendation - do it server side  notice my comments that emphasize "the easiest way to do this does require submitting the form"

The only reason I mentioned iFrames was as an alternative that would not submit the whole form as that is what was originally asked, but I still think the best method is to just submit the form and then re-create it if there is a problem.
0
 
LVL 6

Expert Comment

by:usankar
ID: 10955701
gaby22 - here is what you wanted
do a query to list down all the US and cananda area codes and prefixes, concatenate them with a separator and store them as a form hidden field in your form
<input type="USprefix" value="123,234,827,2828">
<input type="Canadaprefix" value="123,234,827,2828">

Once the user keys in the area code and leaves the field, cross check against the US and canada hidden fields using javascript,
If valid then allow further, not throw error message accordingly.
Hope this helps
0
 
LVL 35

Expert Comment

by:mrichmon
ID: 10960813
I mentioned in earlier comments that this could be done (actually it would be better to store in a javascript variable instead of a hidden form field), but if the database of areacodes is huge then you are vastly increasing the page size AND it is a slow process to search to findthe areacode you need to validate against.

The best method is just to submit...
0
 

Author Comment

by:gaby22
ID: 10963574
Thank you mrichmon for all the info.  I'm trying to implement the iFrame solution. However I don't know how to send the value of the area code "and" the prefix to be validated against the db.
<input name="AHPhone" type="text">                  
<input name="HPhone1" type="text">

I see that the function send "i" as value Can I send the 2 inputs?
0
 
LVL 35

Expert Comment

by:mrichmon
ID: 10963833
The reason I send I is that I dynamically name the inputs.  If you know that the inputs will be named AHPhone and HPhone then you don't need to send i at all.

Your function would look something like :

function LookupAreacode()
{
     var Areacode= document.formname.AHPhone.value;
     var prefix = document.formname.HPhone1.value;
         
     if (areacode != "")
     {     // If an areacode was entered then look it up in our DB
          callToServer("RPC_AreacodeLookup.cfm?AHPhone=" + escape(Areacode) + "&HPhone1=" + escape(prefix));
     }
}

Be sure to replace the "formname" with your form's actual name
0
 

Author Comment

by:gaby22
ID: 10989917
If I want to validate after submiting, what will be the approach??..How do I populate the fields if I need to ask the user to re-enter the phone number.
0
 
LVL 35

Expert Comment

by:mrichmon
ID: 10990556
The validation approach would be something like

Look at the submitted area code and prefix.  Run a query against your database to verify that these are correct areacodes and prefixes.  If so then continue with submit.  If not then set an errorflag and redraw the page  Then ont he page you would populate the fields like this :

<input name="firstname" type="text" value="<cfif ErrorFlag>#Form.firstname#</cfif>">

which would put back the value that had been there
0
 

Author Comment

by:gaby22
ID: 11015931
Can anybody give me a sample code to do a server-side validation of the phone number? It needs to query an area code/prefixes db and return to the form if were not found. Pre-fill the fields with the data already entered....Should I post this on a new question??
0
 
LVL 35

Expert Comment

by:mrichmon
ID: 11016012
Well what does the database that stores this information look like?
0
 

Author Comment

by:gaby22
ID: 11016106
This is the query I'm using to check phone numbers already in file.
<cfset dacode = "#left(Hphone,3)#">
<cfset recprefix = "#mid(Hphone,4,3)#">

<CFQUERY datasource="codespref" name="codes">
SELECT *
FROM rightcodes
WHERE areacode = '#dacode#' and prefix = '#recprefix#'
</CFQUERY>
<cfif codes.RecordCount EQ 0>
<cfset mess = "Bad Phone" >
 <cfelse>
<cfset mess = "OK" >
</cfif>
0
 
LVL 35

Accepted Solution

by:
mrichmon earned 500 total points
ID: 11016254
Okay well if you already have that then you really already have the test.

You would do something like this :

<cfset ErrorFlag = false>
<cfif IsDefined("Form.submitbtn")>
     <cfset dacode = left(Hphone,3)> <!--- notice you don't need the quotes nor the # here in the cfsets --->
     <cfset recprefix = mid(Hphone,4,3)><!--- notice you don't need the quotes nor the # here in the cfsets --->

     <CFQUERY datasource="codespref" name="codes">
          SELECT *FROM rightcodes WHERE areacode = '#dacode#' and prefix = '#recprefix#'
       </CFQUERY>
      <cfif codes.RecordCount EQ 0>
         <cfset ErrorFlag = true>
      </cfif>
     <cfif NOT ErrorFlag>
        process form hereand do a cflocation to next page when done
     </cfif>
</cfif>

<html>
<head><title>Test Phone NUmber Page</title></head>
<body>
<form name="testphone" method="post" action="thispage.cfm">
  <cfif ErrorFlag>Please enter a valid phone number including areacode and prefix</cfif>
  Phone Number: <input name="HPhone" type="text" value="<cfif ErrorFlag>#Form.HPhone#</cfif>">
   <input type="submit" name="submitbtn" value="Test">
</form>
</body>
</html>
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

In our day to day coding, how many times have we come across a necessity to check whether a URL is a broken link or not? For those of you that answered countless and are using ColdFusion like myself, then this article is for you.  It will show yo…
Introduction This article explores the design of a cache system that can improve the performance of a web site or web application.  The assumption is that the web site has many more “read” operations than “write” operations (this is commonly the ca…
This video discusses moving either the default database or any database to a new volume.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now