469,359 Members | 1,689 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,359 developers. It's quick & easy.

RegEx help in Javascript?

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?


Jul 23 '05 #1
4 1018

"Noozer" <do*******@me.here> wrote in message
news:o0tye.1867503$Xk.369504@pd7tw3no...
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"

???
Jul 23 '05 #2
Noozer wrote:
I have a variable named "acct". I first want to remove any "-" characters
from it's value.


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

Daniel
Jul 23 '05 #3
Noozer wrote:
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")
Jul 23 '05 #4
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('-') );

">
--
Rob
Jul 23 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

11 posts views Thread by Lord Khaos | last post: by
6 posts views Thread by Vishant | last post: by
3 posts views Thread by Chris | last post: by
2 posts views Thread by Mr.Clean | last post: by
reply views Thread by Chris McKenzie | last post: by
13 posts views Thread by The Cleaning Wonder Boy | last post: by
2 posts views Thread by shapper | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.