Solved

How do I keep a confirmation message from firing when changing a text city field, when the field is empty?

Posted on 2008-10-03
7
178 Views
Last Modified: 2010-04-21
I am working on an issue where we need to update a customer's address.  When you change the city, state, or zip, a message is fires that says "Do you want to change the city from priorCity to currentCity".  If the current city.  This is working correctly, however, if the field is initially blank, or if you remove the listed city, confirm that changing it to blank is OK, then type in a fresh city name, the message fires and it is not supposed to.  

I tried changing the if statement from  if (priorCity != currentCity)
        {

to

 if ((contactInformationForm.elements[priorCityField].value.length != 0) && priorCity != currentCity)
        {...

then loaded my test server, the city was set to "Phoenix" I erased it and pressed tab, the message fired. Good.  Then I typed in a new city name and the message didn't fire, which is what I want, but then I changed THAT city to something else, and the message doesn't fire at all anymore.  It's still thinking the length of the field is 0.  
from the jsp I have - 
 

                    	property="property(city)" 

                    	onblur="isAlphaWithSpacesOnly(this);javascript:checkCityStateZip(this);checkCity(this);"" 

                    	onchange="setChanged();

                       	errorId="property(addressId)"/>
 

from the js i have -
 

function checkCity(field)

{

    if (zipLookupDone == "true")

    {

        var priorCity = contactInformationForm.elements[priorCityField].value;

        var currentCity = contactInformationForm.elements[cityField].value;

        if (priorCity != currentCity)

        {

            

            if (!confirm("Should the city be changed from " + priorCity + " to " + currentCity + "?"))

            {

                contactInformationForm.elements[cityField].value = priorCity;

            }

            else

            {

             contactInformationForm.elements[priorCityField].value = currentCity;

            }

        }

    }

}

Open in new window

0
Comment
Question by:Bishork
  • 5
  • 2
7 Comments
 
LVL 10

Expert Comment

by:webwyzsystems
ID: 22637326
Check the jsp part...Fix your quotes around the onChange() handler and end of previous line perhaps?
Does the global zipLookupDone contain true? Proper quotes around the field names you are referring to in the elements array?
0
 

Author Comment

by:Bishork
ID: 22638069
oops, the jsp should be as given below -

in the js i have defined

var zipLookupDone = "false";

but it gets overriden if there is already a value in the zip field

    if (contactInformationForm.elements[zipField].value.toString().length > 0)
    {
          zipLookupDone = "true";
    }

so when i load the page and the address is already filled in, ziplookupdone should be true.  but what my tester is telling me (i havent tried to test this yet), is taht even when the fields are all initially blank, the confirmation message is still firing when changing from empty field to entering a city name.  

Lets say you have: City:_____  and you change it to City: Beverly Hills
you get "Do you want to...yes or no"

That shouldn't happen if the field is blank.  Only if its City: San Diego and you change it to City: Los Angeles

                    <cf-el:text 

                    	property="property(city)" 

                    	onblur="isAlphaWithSpacesOnly(this);javascript:checkCityStateZip(this);checkCity(this);" 

                    	onchange="setChanged();"

                       	errorId="property(addressId)"/>

Open in new window

0
 

Author Comment

by:Bishork
ID: 22638170
with if ((contactInformationForm.elements[priorCityField].value.length != 0) && priorCity != currentCity)
if the initial value of the field is "phoenix" and i erase it, confirm, then change the blank field to "calexico" it wont fire.  this is good.  but then if i change it again from calexico to "prescott" it wont fire.  
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:Bishork
ID: 22638223
i also tried if (((priorCity != null) || (priorCity != "")) & priorCity != currentCity)
and that didnt have any affect whatsoever from just using priorCity != currentCity alone
0
 
LVL 10

Accepted Solution

by:
webwyzsystems earned 500 total points
ID: 22638344
Why not put your checkCity() function in the onChange handler? I think it would be called more reliably there.
Also - setup priorCity as a global javascript variable instead. Right now it's scope is limited to the function. So...something like:



var priorCity= ""; // or populate with existing data on document load
 

function checkCity(field)

{

  if (priorCity !=0){

    if (zipLookupDone == "true")

    {

        var currentCity = contactInformationForm.elements[cityField].value;

        if (priorCity != currentCity)

        {

            

            if (!confirm("Should the city be changed from " + priorCity + " to " + currentCity + "?"))

            {

                contactInformationForm.elements[cityField].value = priorCity;

            }

            else

            {

             priorCity = currentCity;

            }

        }

    }

  }

}

Open in new window

0
 

Author Comment

by:Bishork
ID: 22673093
thanks for your help, that got me on the right track.  i ended up with  the following - which works!
function checkCity(field)

{

        //isAlphaWithSpacesOnly(field);

        var priorCity = contactInformationForm.elements[priorCityField].value;

        var currentCity = contactInformationForm.elements[cityField].value;  
 

    if (isAlphaWithSpacesOnly(field))

    {

     if (priorCity != 0) 

     {

      if (zipLookupDone == "true")

      {

  

        if (priorCity != currentCity)

        {

            if (!confirm("Should the CITY be changed from " + priorCity + " to " + currentCity + "?"))

            {

                contactInformationForm.elements[cityField].value = priorCity;

            }

            else

            {

             contactInformationForm.elements[priorCityField].value = currentCity;

            }

        }

      }

    }

     else

     {

     contactInformationForm.elements[priorCityField].value = currentCity;

     }

   }

   else

   {

   return false;

   }   

}

Open in new window

0
 

Author Closing Comment

by:Bishork
ID: 31504428
thanks, that got me on the right track.  i ended up w/ the following, which works.

function checkCity(field)
{
        //isAlphaWithSpacesOnly(field);
        var priorCity = contactInformationForm.elements[priorCityField].value;
        var currentCity = contactInformationForm.elements[cityField].value;  

    if (isAlphaWithSpacesOnly(field))
    {
     if (priorCity != 0)
     {
      if (zipLookupDone == "true")
      {
 
        if (priorCity != currentCity)
        {
            if (!confirm("Should the CITY be changed from " + priorCity + " to " + currentCity + "?"))
            {
                contactInformationForm.elements[cityField].value = priorCity;
            }
            else
            {
             contactInformationForm.elements[priorCityField].value = currentCity;
            }
        }
      }
    }
     else
     {
     contactInformationForm.elements[priorCityField].value = currentCity;
     }
   }
   else
   {
   return false;
   }  
}
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

The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…

707 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

18 Experts available now in Live!

Get 1:1 Help Now