Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 05-04-2010, 03:25 PM   PM User | #1
vitugja
New to the CF scene

 
Join Date: May 2010
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
vitugja is an unknown quantity at this point
Validation on a field for a "last name" while allowing for an Apostrophe

Hi guys,

I am trying to re-work some pre-written Javascript code at which I am a novice at (I'm more CSS, HTML, PHP...). I am trying to validate a form field for a Purchaser's last name while accounting for an apostrophe if they have one in their last name.

I bet it's something simple, but I still haven't been able to figure it out, or write it correctly:


Code:
function checkPurchaserName()
{
  var err = false;
  var f = $(getApogeeElement('purchaser_name_first'));
  var l = $(getApogeeElement('purchaser_name_last'));

  if ( f && l &&
       ( f.value.toLowerCase() != first.toLowerCase() ||
         l.value.toLowerCase() != last.toLowerCase() ) )
  {
    error_list[error_list.length] = 'The name of the purchaser must match the applicant.';
    errorHighlight(f);
    err = true;
  }

  return err;
}
As an example. I started the purchasing process with the name "Patrick O'Malley".

At the end page where this validation occurs, they have (2) text boxes, one for "first name" and one for "last name". They must type the same exact name for purchaser that they did for applicant, so "Patrick", then "O'Malley"

Can someone please put me on the right track?

Thanks.
vitugja is offline   Reply With Quote
Old 05-04-2010, 03:42 PM   PM User | #2
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,103
Thanks: 197
Thanked 2,421 Times in 2,399 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
The code you posted simply requires that the purchaser name must match the applicant name (what is the point of that?) whatever that name is.

Are you sure that somewhere along the line the apostrophes are not being filtered out? (a-z only) Some names have hyphens as well. Patrick O'Malley-Jones.


"When the questions are being asked, that's when you come up with the answers." Prospective MP
Philip M is offline   Reply With Quote
Old 05-04-2010, 03:59 PM   PM User | #3
vitugja
New to the CF scene

 
Join Date: May 2010
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
vitugja is an unknown quantity at this point
The point of matching the typed in Purchasing Name to the Applicant name is that we're following state guidelines for a product that we're selling. Can't really get into that right now.

I have PHP for the initial part of the application that allows for apostrophes and hyphens, and if the last name has an apostrophe or hyphen in it, that it doesn't make the second part of the last name lowercase:

Code:
$temp = strtolower("<#policyholder.last_name#>");
    $temp = join("'", array_map('ucwords', explode("'", $temp)));
    $temp = join("-", array_map('ucwords', explode("-", $temp)));
    $result = $temp;
It looks like that javascript validation that I inserted previously is now converting everything to lowercase, and doesn't allow for special characters. Something I am trying to get help on.

I forgot to mention that the javascript validation allows the hyphenated last name to go through to purchase completion, but when I have a last name with an apostrophe, it gives me the error message, saying the purchaser name does not match the applicant. That's why I'm stumped.

Last edited by vitugja; 05-04-2010 at 04:12 PM.. Reason: forgot pertinent information
vitugja is offline   Reply With Quote
Old 05-04-2010, 04:16 PM   PM User | #4
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,103
Thanks: 197
Thanked 2,421 Times in 2,399 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
I don't do php, but the Javascript method toLowerCase() has no effect on hyphens or apostrophes.

Code:
var n = "Patrick O'Malley-Jones";
n = n.toLowerCase();
alert (n);
Try escaping the ' with a backslash \'.
Code:
n = n.replace(/'/,"\'");
Philip M is offline   Reply With Quote
Old 05-04-2010, 04:27 PM   PM User | #5
vitugja
New to the CF scene

 
Join Date: May 2010
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
vitugja is an unknown quantity at this point
I guess this is where I'm confused. I tried something like this earlier, but I believe I'm just putting this in the wrong spot. Where would I put:
Code:
n = n.replace(/'/,"\'");
in this:

Code:
function checkPurchaserName()
{
  var err = false;
  var f = $(getApogeeElement('purchaser_name_first'));
  var l = $(getApogeeElement('purchaser_name_last'));

  if ( f && l &&
       ( f.value.toLowerCase() != first.toLowerCase() ||
         l.value.toLowerCase() != last.toLowerCase() ))
  {
    error_list[error_list.length] = 'The name of the purchaser must match the applicant.';
    errorHighlight(f);
    err = true;
  }

  return err;
}
Thanks for your help. Very much appreciated.
vitugja is offline   Reply With Quote
Old 05-04-2010, 04:30 PM   PM User | #6
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,103
Thanks: 197
Thanked 2,421 Times in 2,399 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Code:
var f = $(getApogeeElement('purchaser_name_first'));
f = f.replace(/'/,"\'");
var l = $(getApogeeElement('purchaser_name_last'));
l = l.replace(/'/,"\'");
For what it is worth, it is not a good idea to use the unadorned letter l as a variable as it is too easily confused with the number 1. Likewise O and 0.

Why are you converting the names to lower case anyway? You said They must type the same exact name for purchaser that they did for applicant, so "Patrick", then "O'Malley", not "patrick" and "o'mALLey".

Last edited by Philip M; 05-04-2010 at 04:33 PM..
Philip M is offline   Reply With Quote
Old 05-04-2010, 04:37 PM   PM User | #7
vitugja
New to the CF scene

 
Join Date: May 2010
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
vitugja is an unknown quantity at this point
Thanks. That's what I tried earlier, and it erased the whole last name and left this on the screen:
Code:
/'/
I'm thinking I need to append it to something else in the code.
vitugja is offline   Reply With Quote
Old 05-04-2010, 04:40 PM   PM User | #8
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,103
Thanks: 197
Thanked 2,421 Times in 2,399 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Quote:
Originally Posted by vitugja View Post
Thanks. That's what I tried earlier, and it erased the whole last name and left this on the screen:
Code:
/'/
I'm thinking I need to append it to something else in the code.
I think you would do best to post this in the php forum. What you say makes no sense to me.
Philip M is offline   Reply With Quote
Old 05-04-2010, 04:43 PM   PM User | #9
vitugja
New to the CF scene

 
Join Date: May 2010
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
vitugja is an unknown quantity at this point
Hey sorry, didn't see the last part of your message. The conversion with the toLowerCase() function was there previously without me. I'm trying to just pick apart the code and re-write it without breaking anything. If you have a better suggestion with better validation, I'm all "ears". I am really a beginner, so I'm sorry if it seems like I have to be babied a bit about the code.

To give a little more info, without giving too much away. The "purchaser name" is basically an online signature. They are buying a policy online. So that's why the applicant name, and the purchaser name have to match. So that someone else can't be buying the policy for someone else. It also matches their credit card name.
vitugja is offline   Reply With Quote
Old 05-04-2010, 05:55 PM   PM User | #10
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,103
Thanks: 197
Thanked 2,421 Times in 2,399 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Validation of first and last names (basically a-z hyphen apostophe space) has been covered many times in this forum.

But I still do not understand why you are having this trouble of apostrophes being deleted. Is there some prior validation of the "applicant name" which is deleting anything but a-z and hyphen?
Philip M is offline   Reply With Quote
Old 05-04-2010, 08:16 PM   PM User | #11
vitugja
New to the CF scene

 
Join Date: May 2010
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
vitugja is an unknown quantity at this point
Yes, I know it's been covered a million times in this forum. Trust me I was looking at all the posts for about a whole day before I decided to post my own code today to see if it was some little piece of syntax that was preventing my code from working. I definitely wasn't going to post something up here without giving it an honest effort.


You're help was appreciated, but I don't think I can ask for much anymore without other coders being able to see all the modules I'm working with because it seems to be much more than that little piece of JavaScript I posted earlier..... Back to troubleshooting.
vitugja is offline   Reply With Quote
Reply

Bookmarks

Tags
apostrophe, form field, javascript, validation

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 09:58 PM.


Advertisement
Log in to turn off these ads.