RegEx help in Javascript?

N

Noozer

I have a variable named "acct". I first want to remove any "-" characters
from it's value. After this I want to verify that we have only exactly 12
digits in the variable.

Unfortunately I'm pretty green as far as using RegEx.

/\d{12}/.test(acct); should do the second part, but how do I do the first?
 
N

Noozer

Noozer said:
I have a variable named "acct". I first want to remove any "-" characters
from it's value. After this I want to verify that we have only exactly 12
digits in the variable.

Unfortunately I'm pretty green as far as using RegEx.

/\d{12}/.test(acct); should do the second part, but how do I do the
first?

Actually figured out what I needed:

f.Acct.value =
f.Acct.value.replace(/^(\d{4})-?(\d{4})-?(\d{4})$/,'$1$2$3');

BUT, now I need to go the other way...

Assuming that my variable acct contains "123456789012" what expression do I
use to get "1234-5678-9012"

???
 
D

Daniel Kirsch

Noozer said:
I have a variable named "acct". I first want to remove any "-" characters
from it's value.

yourVar = yourVar.replace(/-/g,"");

Daniel
 
D

Daniel Kirsch

Noozer said:
Assuming that my variable acct contains "123456789012" what expression do I
use to get "1234-5678-9012"

replace(/^(\d{4})(\d{4})(\d{4})$/,"$1-$2-$3")
 
R

RobG

Noozer wrote:
[...]
Actually figured out what I needed:

f.Acct.value =
f.Acct.value.replace(/^(\d{4})-?(\d{4})-?(\d{4})$/,'$1$2$3');

Another:

<input type="text" value="123-1233-2345-3" onblur="

// Simple test
alert( 'Has 12 digits? : '
+ /^\d{12}$/.test(this.value.replace(/-/g,'')) );

// Reformat
var str = this.value.replace(/-/g,'');
alert( str.match(/\d{4}/g).join('-') );

">
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top