Help, how can you parse a decimal and ...

K

Karim

I am looking for the easiest way to parse a float number.

X.Y where X=>0 and X <=5 and Y = 0 or Y = 5

e.g. the number should be one of the following 1 1.0 1.5 2 2.0 2.5 etc.

I tired using regular expressions, with no luck.

Thanks,
Karim
 
M

Martin Honnen

Karim said:
I am looking for the easiest way to parse a float number.

X.Y where X=>0 and X <=5 and Y = 0 or Y = 5

e.g. the number should be one of the following 1 1.0 1.5 2 2.0 2.5 etc.

I tired using regular expressions, with no luck.

I think
var numberPattern = /^[0-5]\.[05]$/;
meets your requirements
 
K

Karim Kabbara

Martin,

I tried that, but it only works for 1.0 1.5 2.0 2.5 etc but not 1 2 3 4
5. Is there another way?

Thanks,
Karim
 
L

Lasse Reichstein Nielsen

Karim Kabbara said:
Martin,

I tried that, but it only works for 1.0 1.5 2.0 2.5 etc but not 1 2 3 4
5. Is there another way?

var numberPattern = /^[0-5](\.[05]|)$/;

/L
 
K

Karim Kabbara

I bow to you Lasse.Thanks!

One more addition, I am calculating three fields as-well-as validating.
I came up with the following code.

function SumTotal()
{
var raps = parseFloat(document.Form.score.value);
var lrs = parseFloat(document.Form.score1.value);
var is = parseFloat(document.Form.score2.value);

document.Form.SumTotal1.value = (raps+lrs+is);
}

function checknumeric(field)
{
pattern = /^[0-5](\.[05]|)$/;

if(pattern.test(field.value)==false)
{
alert("You can only use 1 to 5 and .5");
}
}

I would like to add the Checknumeric with each field being entered.
Would I use the function with each field or?

Thanks in advance.

Karim
 
L

Lasse Reichstein Nielsen

Karim Kabbara said:
One more addition, I am calculating three fields as-well-as validating.
I came up with the following code.

function SumTotal()
{

I would make a variable holding the form here, to save time and
space:
var form = document.forms['Form'].elements;
var raps = parseFloat(document.Form.score.value);

and then just write
var raps = parseFloat(form.score.value);
It is not saving a lot, but I think it is easier to read :)

You want the values of these three to be between 0 and 5.5 in one-half
increments. So, you need to check each one.

I assume you want to check them before converting with parseFloat, to
rule out inputs of the form "2.5arglebargle".

For simplicity, I think I would combine the conversion and check:

function checkAndConvert(string) {
if (! (/^[0-5](\.[05])?$/).test(string)) {
alert("The input '"+string+"' is not between 0 and 5.5 in "+
"increments of one half");
return; // returns undefined
}
return +string; // converts to number faster than parseFloat.
}

Then:

function SumTotal() {

var form = document.forms['Form'].elements;

var raps = checkAndConvert(form.score.value);
var lrs = checkAndConvert(form.score1.value);
var is = checkAndConvert(form.score2.value);

if (raps === undefined ||
lrs === undefined ||
is === undefined) {
return;
}

form.SumTotal1.value = (raps+lrs+is);
}
I would like to add the Checknumeric with each field being entered.
Would I use the function with each field or?

In some way, yes.

/L
 
D

Dr John Stockton

JRS: In article <[email protected]>, seen in
news:comp.lang.javascript said:
Karim said:
I am looking for the easiest way to parse a float number.

X.Y where X=>0 and X <=5 and Y = 0 or Y = 5

e.g. the number should be one of the following 1 1.0 1.5 2 2.0 2.5 etc.

I tired using regular expressions, with no luck.

I think
var numberPattern = /^[0-5]\.[05]$/;
meets your requirements

That seems to fit the description.
var numberPattern = /^[1-5](\.[05])?$/; // fits the example

I wonder whether he actually wants 5.5 ?
 
K

Karim Kabbara

Lasse,

Thanks again. It works, but I am using the onChange function, so if I
change each field it calculates all the fields, I need to check the
validation on !this field then add to the total everytime I change to
the next field.

The algorithim would be validate num with rule 0 to 5 with .5
increments. Then add and total the num.

Do you think I need an array to do this?

Regards,
Karim
 
K

Karim Kabbara

Oh yes, I forgot to add, I should use the focus function if the num is
not valid. So when they change focus and it does not satisfy the
condition, it place's them back on the field to change.

Karim
 
L

Lasse Reichstein Nielsen

Karim Kabbara said:
Oh yes, I forgot to add, I should use the focus function if the num is
not valid. So when they change focus and it does not satisfy the
condition, it place's them back on the field to change.

This is a dangerous thing to do. It traps people in the field. Good
thing it isn't the onblur handler, since this way, they can get away
from the input field by trying to leave it twice (the second time,
there is have been no change, so the onchange handler doesn't
trigger). Don't change it to onblur!

You can collect all the values each time you want to update the result,
or you can cache them. Here I cache them in an object instead of in an
array, together with their sum.

---
var values = {raps:0,lrs:0,is:0,total:0};

function checkAndAdd(elem) {
if (!/^[0-5](\.[05])?$/.test(elem.value)) {
alert("Input illegal: "+elem.value);
setTimeout(function(){elem.focus();},20); // [1]
return;
}
var value = +elem.value;
values.total += value - values[elem.name];
values[elem.name] = value;
elem.form.elements['SumTotal1'].value = values.total;
}
---
Then you can call this function from each element:
---
<input type="text" name="raps" value="0" onchange="checkAndAdd(this)">
<input type="text" name="lrs" value="0" onchange="checkAndAdd(this)">
<input type="text" name="is" value="0" onchange="checkAndAdd(this)">
---


[1] The delay is for browsers which change the focus to the next
element *after* executing the onchange event handler, overriding
the focus set in the handler.

Hope this works :)
/L
 
K

Karim Kabbara

Yes, I am not trying to get 5.5 my max is 5. Also, I tried the code and
I am getting Object not found. The sample is below.Thanks

<script language="Javascript">
var values = {raps:0,lrs:0,isc:0,total:0};

function checkAndAdd(elem) {
if (!/^[0-5](\.[05])?$/.test(elem.value)) {
alert("Input illegal: "+elem.value);
setTimeout(function(){elem.focus();},20); // [1]
return;
}
var value = +elem.value;
values.total += value - values[elem.name];
values[elem.name] = value;
elem.form.elements['SumTotal1'].value = values.total;
}


</script>
<table>
<tr>
<td>RAPS</td>
<td><input maxlength="10" size="10" value="" name="raps"
onchange="checkandadd(this)"></td>
</tr>

<tr>
<td >LRS</td>
<td ><input maxlength="10" size="10" value="" name="lrs"
onchange="checkandadd(this)"></td>
</tr>

<tr>
<td >IS</td>
<td ><input maxlength="10" size="10" value="" name="isc"
onchange="checkandadd(this)"></td>
</tr>

<tr>
<td align="right"><span style="FONT-SIZE: 12pt">Overall
Score (0-15):
<o:p></o:p>
</span> </td>
<td ><strong><input id="SumTotal1" readonly name="SumTotal1"
style="WIDTH: 79px; HEIGHT: 22px"

size="10"></strong></td>
</tr>

</table>
 
L

Lasse Reichstein Nielsen

Karim Kabbara said:
Yes, I am not trying to get 5.5 my max is 5. Also, I tried the code and
I am getting Object not found. The sample is below.Thanks
<script language="Javascript">
Use

var values = {raps:0,lrs:0,isc:0,total:0};

function checkAndAdd(elem) {
if (!/^[0-5](\.[05])?$/.test(elem.value)) {

If you didn't want 5.5, the regular expression would have to change, e.g.,
if (!/^(([0-4](\.[05])?)|(5(\.0)?))$/.test(elem.value)) {

or perhaps using a simple comparison with the regular expression:

if (!/^[0-5](\.[05])?$/.test(elem.value) || elem.value > 5) {
elem.form.elements['SumTotal1'].value = values.total;

There is no form around your input elements (which I assumed there
would be), so this fails. Have to change it to:

document.getElementById("SumTotal1").value = values.total;
onchange="checkandadd(this)"></td>

Javascript is case sensitive. This should be

onchange="checkAndAdd(this)"></td>

with capital A's.
This goes for all three inputs.
<td ><strong><input id="SumTotal1" readonly name="SumTotal1"
style="WIDTH: 79px; HEIGHT: 22px"

You don't need the name attribute on this input. Also, the strong tag
isn't affecting the contents of the input in my browser. You would
probably get a better result by adding "font-weight:bold;" to the style
attribute.

/L
 

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

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top