Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Regexp to read number when in * 2

Status
Not open for further replies.

webslinga

Programmer
Jun 20, 2006
55
US
Hey all,
I would like to create a javascript function called checkCCField(). This function should check the credit card # that is placed in a text field called displaydigits and see if there are exactly 8 asterisks (*) in it. If there is not, then change the value of a hidden field called daccount to the one in the text field. If there does turn out to be 8 asterisks in the text field, do nothing.
The biggest problem I am having is how to declare and use my regular expression in my javascript code. The code would probably look something like the following:
Code:
// bookingSearch is my form name
// displaydigits is my text field in which I want to check for the credit card account
// display digits is the hidden field value that I want to change is I cannot match the Regex pattern
function checkCCField()
{
  if(document.bookingSearch.displaydigits <not matching> <insert Regex pattern here for 8 asterisks??>)
  {
    document.bookingSearch.daccount.value = document.bookingSearch.displaydigits;
  }
}

Any feedback would be appreciated on this topic. Since I also do not have experience with js, if there is anything wrong with the logic
of the code above, I would appreciate checking into that too. Thanks in advance.
 
You need to be a bit more specific about the pattern you are trying to match. What goes between the 8 asterisks? Most credit cards don't have asterisks in them, so I'm not sure exactly what you're trying to accomplish. Show some example strings of what should match the pattern and what shouldn't match the pattern.

-kaht

[small](All puppies have now found loving homes, thanks for all who showed interest)[/small]
 
Are you referring to a password field? I don't think it's possible to incorporate regexs with password fields. perhaps just get the length of the value and check for that?



*cLFlaVA
----------------------------
[tt]"quote goes here"[/tt]
[URL unfurl="true"]http://www.coryarthus.com/[/url]
 
Yep I understand where you're coming from. Let me be more clear. The strings in the text field displaydigits will have the following samples as values:

1234********1234
4111********6789

This is because I do not want the users of my application to be able to see the entire number. However, a user can enter a fully qualified credit card number into the text field such as:

1234567891035466

In this case, I want this value to replace whatever is in my hidden field called daccount. I hope that is more clear. Thanks.
 
If you're specifically trying to find out whether or not you have 4 numbers followed by 8 asterisks followed by 4 numbers then this regex will work:
Code:
function testMe(str) {
   alert([b]/^\d{4}\*{8}\d{4}/[/b].test(str));
}

-kaht

[small](All puppies have now found loving homes, thanks for all who showed interest)[/small]
 
I want to be able to evaluate the textfield upon submit if that would help understanding my problem. kaht, that regex pattern looks perfect. I was concerned with what type of regex matching system JS follow(PCRE OR POSIX).

Now, I just want to be able to place the value in a hidden value if the pattern above does not match. This function has to be evaluated upon submit. Hope that helps.
 
storing a value in a hidden field is simple:

Code:
function testMe(str) {
   if (!(/^\d{4}\*{8}\d{4}/.test(str))) {
      document.getElementById("hiddenFieldId").value = str;
   }
}



*cLFlaVA
----------------------------
[tt]"quote goes here"[/tt]
[URL unfurl="true"]http://www.coryarthus.com/[/url]
 
I left something off the regexp:
Code:
/^\d{4}\*{8}\d{4}[!]$[/!]/

Otherwise this would validate:

[tt]1234********1234akn.anmxzb8y(*&^A(7tl;mn.24t[/tt]

-kaht

[small](All puppies have now found loving homes, thanks for all who showed interest)[/small]
 
clFlava,
The logic is correct but I would like to test a text field input value instead of an arbitrary string.

How can I do that? Yeah, I know. I need help with my JS.
 
depends how you're trying to call the function.

what do you have so far?

to isolate the value of a particular element, use:

Code:
var str = document.getElementById("yourTextElementId").value;



*cLFlaVA
----------------------------
[tt]"quote goes here"[/tt]
[URL unfurl="true"]http://www.coryarthus.com/[/url]
 
I was thinking about doing something like this:
Code:
function checkCCfield(formName)
{   
   if(!(/^d{4}\*{8}\d{4}$/).test(formName.displaydigits.value))
   {
    formName.daccount.value = formName.displaydigits.value
   }
}

and in my HTML:

Code:
<form onload="checkCCfield(this.form)" name="bookingSearch" id="bookingSearch" method="post" action="/finance/chargecard">
...

What do you think?
 
i think a few things.

first, when in a form tag, i don't think this.form returns what you think it does... all you need is "this".

second, i'm pretty sure you want the onsubmit event, not the onload event.

try something like this:

Code:
function checkCCfield(f)
{
    if(!(/^d{4}\*{8}\d{4}$/).test(f.elements['displaydigits'].value)) {
        f.elements['daccount'].value = f.elements['displaydigits'].value;
   }
}


...


<form onsubmit="checkCCfield(this)" name="bookingSearch" id="bookingSearch" method="post" action="/finance/chargecard">



*cLFlaVA
----------------------------
[tt]"quote goes here"[/tt]
[URL unfurl="true"]http://www.coryarthus.com/[/url]
 
That worked... I had some trouble with the pattern matching when I noticed that the first d wasn't prefixed by a /.


Anyways, works great now. Thanks to all who contributed!
 
no purple love for me.... guess it was because he didn't copy/paste my code correctly *shrug*

-kaht

[small](All puppies have now found loving homes, thanks for all who showed interest)[/small]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top