f



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?




0
Noozer
7/5/2005 10:10:28 AM
comp.lang.javascript 38370 articles. 0 followers. javascript4 (1315) is leader. Post Follow

4 Replies
1196 Views

Similar Articles

[PageSpeed] 3

"Noozer" <dont.spam@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"

???


0
Noozer
7/5/2005 10:21:13 AM
Noozer wrote:
> I have a variable named "acct". I first want to remove any "-" characters
> from it's value.

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

Daniel
0
Daniel
7/5/2005 10:22:32 AM
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")
0
Daniel
7/5/2005 10:36:51 AM
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
0
RobG
7/5/2005 10:45:54 AM
Reply:

Similar Artilces:

javascript within a javascript
Hello, I have a multi-frame page. The frames are named Frame_1, Frame_2 and Frame_3. Frame_1 has a drop down box. When a value is selected in this drop down box, Frame_2 is updated using an asp file (as I have to read from a back-end access database). In turn in the asp file, I have a Body onload tag to execute a javascript function within Frame_2. this function updates the contents of Frame_3. Till now this is working fine. Here is the problem. Frame_3 has a form with a radio button. When a radio button is selected, I want that a function be executed in Frame_3. However since conte...

Javascript and IE? Javascript and C#?
While my question might be simple, the environment around it is terribly messy and so I will try to keep this clear and simple by only including the relevant code - however, as I will soon suggest, I worry that the problem isnt in what would seem to be the relevant code but instead is lost somewhere in the do-and-donts of the peripheral code. So at the end, i'll try to include all the affected code and you can see if any of its actually relevant. I know there is a lot going on... sorry, but I tried to write this clearly. So here is what I imagine to be relevant. I'm working with a pa...

regex and Javascript
Hi - I've never tried using regular expressions with JS. Can someone help me with a simple one? I have a field that a user inputs that looks for an integer, or a some integers with a decimal then does some calculations and converts it to currency. Problem is, that if i have to recalculate, that field now has a "$" in it which throws off calculations. I'd like to look at the field and if there is a "$", strip it out, do the calculations, etc. So, something like: function getDollar() { myAmount = window.document.form.amount.value; //here is where i want to look f...

JAVAScript help
I am new to javascript and can't figure out how to write a shopping cart function - i have some basic code, but don't completely understand how it works. i can't find anywhere online that has an example of code written for a shopping cart of anytype for a website - any info would be greatly appreciated! ranestorm wrote on 27 okt 2003 in comp.lang.javascript: > i can't find anywhere online that has an example of code written for a > shopping cart of anytype for a website - <http://www.google.com/search?q=javascript+%22shopping+cart%22> 300.000 hits ! -- Evertj...

dynamic load javascript from javascript
hi, from here: http://www.activewidgets.com/javascript.forum.6114.15/dynamic-load-javascript-from-javascript.html there is a method to load js from js, e.g. //--------------------------------------------------------------------------------------- var script = document.createElement('script'); script.type = 'text/javascript'; script.src = 'snip.js'; document.getElementsByTagName('head')[0].appendChild(script); //--------------------------------------------------------------------------------------- this method works great, but there is a bug(?) when using wit...

regex in javascript
Hi folks, I'm trying to learn and understand regex in javascript for web form validation. In trying to write a piece of script to validate an email address I came up with the following: function isEmail(input) { var re =/^\w([\.\-]\w)*@\w([\.\-]\w)+$/ if (!re.exec(input)) { return false } } This is then referred to as : if (!isEmail(document.form.mailfrom.value)) { alert("This does not look like an email address") } etc. Unfortunately it bombs on absolutely everything. Can anyone suggest why? As an aside, is there any trul...

Javascript help
Hello, I am trying to validate the format of telephone number (xxx-xxxx) This is what i have so far (which also has other validations such as check to see if there are any inputs in the text box-but that script works) function fncTest() { x=document.UpdateInput set=/^\d{3}-\d{4}$/ if (!x.txtPhone.match(set)){ alert( "Please phone number" ); return false; } } is there something wrong with my syntax? Please help.... Thank you... smilelots4@yahoo.com writes: > I am trying to validate the format of telephone number (xxx-xxxx) > This is what i have so far (which also has othe...

help on javascript:
I got the problem on passing the selected index of drop list to the javascript function as below: onchange:javascript:generateURL(servername, this.options.[selectedIndex].value, docID) where i want to generate something like http://www.abc.com?param?imagecursor=docID#0001 where servername = www.bac.com imagecursor=docID+selectedIndex padded with 0 "timothy ma and constance lee" <timcons1@shaw.ca> wrote in message news:3Ocfd.22441$%k.11206@pd7tw2no... > I got the problem on passing the selected index of drop list to the > javascript function as below: > > o...

Help me out with this JavaScript!
I have a problem with this JavaScript <SCRIPT> var texts = "091c53d492ce7h2"; var interpret = ""; var whatisthis = "var xorm = prompt('Enter the password:','');for (x=1; x<6; x++) {interpret += (texts.indexOf(x)-1);}if (xorm==interpret){interpret = interpret + '.php';location.href=interpret;}else{location.href='hahaha.php';}"; eval(whatisthis); </SCRIPT> which value does "interpret" have after the algorithm? please help me out (i'm a n00b) Thanks SiN wrote on 30 jun 2003 in comp.lang.javascript: ...

regEx in Javascript
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: ------------------------------------------------- function findNumeric(str_obj){ regEx = /\d/; if (str_obj.match(regEx)) return true; else return false; } -------------------------------------------------- The problem arises when I put a password with a space in between e.g: 'test test1'. The fucntion returns false. I've tried '\s' in the regEx but the user can put the space anywhere.. Any idea how to sol...

JavaScript Regex
Dear JavaScripters: I have not found quite the detail that I need for regexes. If you have a URL to a good site, please post it. In particular, how do I group subexpressions? I wish to parse a string to see if it is a valid fixed-decimal value. I start with /^ How do I check a set of characters optionally? I want to check for a sign character. The string might start with "+" or "-", but if it starts with neither, that is fine. Do I leave an empty choice in a parens set? Is it then ([+-]|) or [+-]? ? Then come...

Help With JavaScript
Hi I am new to JavaScript and I am trying the resolve the following problem, can anyone help: ======================================================================================================================================= <html> <head> <title> Die Simulator</title> <script language="JavaScript" type="text/javascript"> // // THIS FUNCTION SIMULATES THE ROLL OF A SINGLE 6-SIDED DIE - YOU SHOULD NOT ALTER THIS FUNCTION // function rollDie () { var result = 0; result = Math.floor((Math.random(...

Some help with Javascript
this code used to show popup image when click to enlarge. Due to weak javascript code used, popup window is not resized to fit image size, not centered, have no close link. I just wanna make this popup auto-resized to the size of the image(with a little buffer space), centered, and with 'close' link. Any help will be appreciated.. <?php /* $Id: popup_image.php */ require('includes/application_top.php'); $navigation->remove_current_page(); $products_query = tep_db_query("select pd.products_name, p.products_image from " . TABLE_PRODUCTS . " ...

javascript
Cut'n'paste from "my" ticket 202 at www.ecomstation.com as they seem to have come to a halt... :-( ################################ Recenty I went from w4 fp17 to eCS 1.2. I get the odd behavior described beneath. Both boxes sees, and uses the same installations and user profiles. Clean install(s) of both Moz and FF has been done several times. Starting FF or Moz clean (no userprofiles), doesn't make any differnce. FIREFOX: The Weather extension i.e doesn't show it's 'mouseover' info, and it refreshes slow. Unit change which ...

Javascript
Recently, I have had the role of being the Javascript guru at work. You know, one peeve I have about COLA is that it seems too oriented around client applications, when the the majority of the world is about Web applications. We talk about OO -- but javascript has always been there. As has DOM, and DHTML. A web developer is a fully OO programmer. In article <5%LCb.2953$Pg1.2136@newsread1.news.pas.earthlink.net> John Bailo <ten@lnilhtrea.oliabaj> wrote: >Recently, I have had the role of being the Javascript guru at work. For single man garage business, you sure have. ...

Javascript
Hello people, I'm trying to make a regional map in html and javascript, I'm sure how to d= o the following: to hover over the city, he should change the city's image with another clea= rer, and clicking in the city he should get a darker color than the picture= when we hover. Until there all right, but when I click it gets darker image and move the m= ouse in another city he immediately removes the dark image and return to th= e clearer picture. How to keep the image when I click on it and still the effect of passing th= e mouse? --------------------- the other t...

JavaScript
Guys, anybody know if is possible "to press" a button linked a javascript function using python? My Firefox send something like that: POST /msgs.aspx TOKEN=B8B83BGDBC191B9FE0A0BE1393294FAB&sig=gpaiOqbp0Nr %2BoecRLF4FGGDOAao %3D&toUserId=1331299&rawAddedDate=1188546666&fromUserId=23029&Action.delete=Send +data python has modules for forms and other things... and for it? Thank you for help... zowtar >python has modules for forms and other things... and for it? Check out httplib and urlib2, it might be useful for you. Hi! I know "how to", but o...

Javascript
Would anyone know whether Javascript works well with sound files? Tina Host wrote: > Would anyone know whether Javascript works well with sound files? Exactly as well as it works with files of any other type. -- |_ CJSonnack <Chris@Sonnack.com> _____________| How's my programming? | |_ http://www.Sonnack.com/ ___________________| Call: 1-800-DEV-NULL | |_____________________________________________|_______________________| ...

JavaScript
If I have Options/ViewingMail/AllowExecutables unchecked does Eudora strip out the JavaScript? Or just refuse to execute it? TIA... Bob "Bob Bailey" <ebiz@usa.net> wrote in message news:rfOdnaRVycZZNzXenZ2dnUVZ_v-dnZ2d@rcn.net... > If I have Options/ViewingMail/AllowExecutables unchecked does Eudora strip > out the JavaScript? Or just refuse to execute it? OK. I've answered my own question but that begs a new one. (Eudora strips the JS.) Having allowed Executables it only works if I open, not preview, the message and then it insists on ope...

javascript
Hi all I wounder if some one can help me i have to write a program that sums numbe3rs together using a for loop. The numbers are 1 to 10 then one to 20 then 1 to 10,000. // A program for summing numbers from one to biggestNumber Var biggestNumber; Var theSum = 0; its got to start like this. Im having problems with this one viicki -- v davies Message posted via JavaKB.com http://www.javakb.com/Uwe/Forums.aspx/java-tools/200512/1 On Fri, 16 Dec 2005 11:27:29 GMT, "vicki via JavaKB.com" <u16404@uwe> wrote, quoted or indirectly quoted someone who said : >// A program fo...

JavaScript
Javascript has it's own significance Amit Patel wrote: > Javascript has it's own significance Mr Patel, Stop using that stuff, it's bad for your brains. :P "Amit Patel" <amit2749@hotmail.com> wrote in message news:5f04ccb8.0401210427.3f93300c@posting.google.com... | Javascript has it's own significance It also has it's own group.. news:comp.lang.javascript ...

Javascript of Google inside my javascript file
Good Morning, I would want to insert the javascript of google adsense in my site, here it is : <script type="text/javascript"><!-- google_ad_client = "pub-4902392511135327"; google_ad_width = 728; google_ad_height = 90; google_ad_format = "728x90_as"; google_ad_channel =""; //--></script> <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script> the problem is that I want to insert it inside a javascript file I already use in a way that I've not to in...

Extract links from Javascript (not using Javascript)?
I am looking for a method to extract the links embedded within the Javascript in a web page: an ActiveX component, or example code in C++/Pascal/etc. I am looking for a general solution, not one tailored to a particular page/script. Hopefully, the problem can be solved without recreating a complete Javascript interpreter. Any ideas? <chrisspencer02@yahoo.com> wrote in message news:1148670236.974770.107310@j55g2000cwa.googlegroups.com... > I am looking for a method to extract the links embedded within the > Javascript in a web page: an ActiveX component, or example code in >...

Nixing Javascript via javascript: pseudoproto?
This is a question about defensive web browsing. Ocassionally I run into a page whose JavaScript does something that I find obnoxious. I would like to turn off JavaScript only for that page (instead of disabling it globally). It would be cool if there were some way to do this through a "bookmarkable" JavaScript snippet using the javascript: pseudoprotocol. Does anyone know any trick to do any of this? Many thanks! -bill "bill" wrote > This is a question about defensive web browsing. Ocassionally I > run into a page whose JavaScript does something that I fi...

Web resources about - RegEx help in Javascript? - comp.lang.javascript

JavaScript Kit- Your comprehensive JavaScript, DHTML, CSS, and Ajax stop
Your comprehensive JavaScript, DHTML, CSS, and Ajax stop

Let's Code: Test-Driven Javascript by James Shore — Kickstarter
James Shore is raising funds for Let's Code: Test-Driven Javascript on Kickstarter! A screencast series focusing on rigorous, professional Javascript ...

Eloquent JavaScript: A Modern Introduction to Programming
... and pragmatics. I loved the tutorial-style game-like program development. This book rekindled my earliest joys of programming. Plus, JavaScript! ...

Lightbox (JavaScript) - Wikipedia, the free encyclopedia
On a Lightbox-enabled page, a user can click an image to have it magnified in a Lightbox window, which resizes itself according to the size of ...

Open Directory - Computers: Programming: Languages: JavaScript: Tools: Obfuscators
about dmoz - help Top : Computers : Programming : Languages : JavaScript : Tools : Obfuscators Tools for obfuscation/compression JavaScript code. ...

Facebook Open-Sources JavaScript Static-Type Checker Flow
The latest software to be open-sourced by Facebook is Flow , a static-type checker for JavaScript . (more…) New Career Opportunities Daily: ...

Under the Hood: The JavaScript SDK - The use of polyfills - Facebook
Facebook Engineering hat eine Notiz mit dem Titel Under the Hood: The JavaScript SDK - The use of polyfills geschrieben. Du kannst den vollständigen ...

Quickstart: Facebook SDK for JavaScript
This quickstart will show you how to setup the SDK, load social plugins, trigger a dialog, and get it to make some basic Graph API calls.

Facebook App Devs Get Javascript
It has been an oft-lamented fact of the Facebook Platform thus far in its life that, in order to use all but the most simple of Javascript functionality–the ...

Badass JavaScript (@badass_js) on Twitter
WebGLWorker is an open source library that allows using WebGL from Web Workers. Great work as always from @kripken . blog.mozilla.org/research/2014/… ...

Resources last updated: 2/28/2016 11:37:56 AM