• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 358
  • Last Modified:

Strip string of none-numeric characters

Hiyas,

I'm having troubles making a calculation from a form. When inputs are filled they aren't always pure numeric, and you can't calculate letters... Fields sometime contains both numbers and alphabetic characters, it looks like this:

---the form---
[code]
<form name=form1>
<input type=text name=int1 value=195>
<input type=text name=int2 value=65>
<input type=text name=int3 value=ZR15>
<input type=text name=int4 value='' onfocus="calcforme();">
</form>
[/code]

---javascript calculation---
[code]
<script language=javascript>
function calcforme(){

      arg = /[^A-Z| ]/g //<---------HERE'S THE PROBLEME. I don't know how to set arg properly

      //Globals
      var tf = getElement("form1"); //external function
      var dim = tf.elements['int1'].value.replace(arg, "");
      var profil = tf.elements['int2'].value.replace(arg, "");
      var bredd = tf.elements['int3'].value.replace(arg, "");
      //Locals
      var pi = 3.14;
      var tum = 25.4;//mm
      var hojd;
      var omkrets;

      hojd = ( ( dim * tum ) + ( profil * 2 ) * ( bredd / 100 ) );
      omkrets = hojd * pi;

      tf.elements['int4'].value=parseInt(omkrets);

}
</script>
[/code]

As you might understand, I've tried to set arg to contain only alphabetic characters, also I realized I've failed. Actually, I suck.. I've never figured out how it works with /[]/g. So if someone could tell me about it aswell I'd appericiate it a lot.

Thanks,
Klodorff
0
Klodorff
Asked:
Klodorff
  • 3
  • 2
  • 2
1 Solution
 
str_kaniCommented:
arg = /^([A-Za-z]+)$/;

this will accept only character,

arg = /^([0-9]+)$/; will accept only numbers..

Hope this helps
0
 
str_kaniCommented:
0
 
BatalfCommented:
The correct syntax for

arg = /[^A-Z| ]/g

should be

arg = /[^0-9]/g

if you want to strip off everything except numbers.

Batalf
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
KlodorffAuthor Commented:
Thank you for fast reply.

I now understand a bit better how it works :)

Still it doesn't work as I want it to, though.

-----Illustration:
[code]
arg1 = /^([A-Za-z]+)$/;
arg2 = /^([0-9]+)$/;

var str1tmp = "ABC123";
var str2tmp = "ABC";
var str3tmp = "ABC123";
var str4tmp =  "123";

var str = str1tmp.replace(arg1,""); // str: ABC123
var str = str2tmp.replace(arg1,""); // str: empty

var str = str3tmp.replace(arg2,""); // str: ABC123
var str = str4tmp.replace(arg2,""); // str: empty
[/code]

Here we got some testing. And I figured: As long ABC and 123 are not in the same string arg1 and arg2 can do their work. But as soon the string contains both integers and alphabetics nothing happens to the string. Maybe replace() is the wrong function to use? I have no idea.

/Klod
0
 
BatalfCommented:
If you want to end up with numbers, then you don't need more than one simple regexp, this one

arg = /[^0-9]/g

example:

arg = /[^0-9]/g
var str1tmp = "ABC123";
alert(str1tmp.replace(arg,''));

btw: arg = /[^0-9]/g could also be written arg = /[^\d]/g
Batalf
0
 
KlodorffAuthor Commented:
There we go, thanks Batalf. First post ownd my code!

Great 2:nd explanaiton too :)

str_kani, great links! At last I get a chance to understand regexp.
0
 
BatalfCommented:
Glad I could help!

Batalf
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 3
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now