473,396 Members | 2,087 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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 1109

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Lord Khaos | last post by:
If I am trying to find an expression, foo, I can do something like this: rExp = /foo/gi; if(results.search(rExp) > -1){ and all work fine. however, if I want my search term to be a...
3
by: Jon Maz | last post by:
Hi All, Am getting frustrated trying to port the following (pretty simple) function to CSharp. The problem is that I'm lousy at Regular Expressions.... //from...
6
by: Vishant | last post by:
Hi, I'm new to javascript and regEx and trying to solve the following problem. I have a function which validates the password if there is a number:...
3
by: Chris | last post by:
I need to write a function that will remove a specified parameter from a URL. For example: removeParam("param1", "http://mysite.com/mypage.htm?param1=1&param2=2"); would return: ...
2
by: Mr.Clean | last post by:
I am working on modifying a syntax highlighter written in javascript and it uses several regexes. I need to add a language to the avail highlighters and need the following regexes modified to...
0
by: Chris McKenzie | last post by:
Hi, I'm using IE 6, and I'm doing some RegEx replacement on the client. Here's my code: regExStr = "\\d{2}:\\d{2}"; // where pattern matches ##:## regExStr += "(?=\\s)"; // and the next...
13
by: The Cleaning Wonder Boy | last post by:
Could someone please explain to me what the (?<Key> and (?<Value> are in the following Regex expression? This gets relative links in an HTML string (file). ...
3
by: jab3 | last post by:
Hello. I"m new to this group, and to JavaScript in general, so please forgive me if I breach local etiquette. I'm trying to implement some client-side 'dynamic' validation on a form. I'm having...
2
by: shapper | last post by:
Hello, I am trying to validate a value using Regex. How can I do this with Javascript? Thanks, Miguel
9
by: kummu4help | last post by:
can anyone give me a regex to validate the password with following conditions hope i am clear. i tried with ctype_alnum() function in php but it is accepting if all characters or either...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.