Solved

# IsNumeric allow certain characters, £.,

Posted on 2004-11-05
Medium Priority
415 Views
Hi i want to be able to use the function below, which i got from an ee question:-

function IsNotNumeric(sText)
{
var ValidChars = "0123456789£.,";
var IsNotNumber=false;
var Char;

for (i = 0; i < sText.length && IsNotNumber== false; i++)
{
Char = sText.charAt(i);
if (ValidChars.indexOf(Char) == -1)
{
IsNotNumber= true;
}
}
return IsNotNumber;
}

Which is fine, but i want to be able to validate the form if submit and the field only contains £., then error message but if \$., and a numeric value, then fine.

Thanks

Ross
0
Question by:rossh1977
• 3
• 2
• 2

LVL 13

Assisted Solution

StormyWaters earned 1000 total points
ID: 12504152
What an odd function...
This will return true if the field contains £ then some digits.
valid inputs include £1.22, £1, £22.341. Do not include 1.22, .45

function testMoney(val) {
var re = /£[0-9]{1}[0-9,.]?/;
return re.test(val);
}
0

LVL 31

Accepted Solution

GwynforWeb earned 1000 total points
ID: 12505411
try this

<script>
function isNumeric(val) {
val=val.replace(/(^£)|(£\$)/,'')
return !isNaN(val);
}

//TRUE VALUES

val='£234234.9800'

val='£23420'

val='234234.9800£'

val='234234.9800'

//FALSE VALUE

val='\$234234.9800'

val='234234.9800\$'

val='23£420'

val='£234234.9800£'

val='234234.9800\$'

val='234234.98ABCD'
</script>

0

LVL 31

Expert Comment

ID: 12505430
the above is easily fine tuned for any subcases that I may have wrong
0

Author Comment

ID: 12505575
That's great thanks bery much.  This is probably daft, but i was wanting the user to enter a currency value, if their are symbols (£.,) remove them using the function below php/mySql:-
...
function tidy_money(\$search)
{
\$search = ereg_replace("[^[:digit:]]", "", \$search);

return \$search;
}

\$query = "INSERT INTO research (research_id, amountAwarded) VALUES (0, '".tidy_money(\$_POST['amountAwarded']."'))";
...
and then when it come to display the result:-

i'm using:-

function convert_to_money( &\$s, \$dec=2 )
{
// format integer number to display as cash
\$f = floatval( \$s );
if( \$f<0 ):
\$p = array( '(£', ')' );
\$f = str_replace( '-', '', \$f );
else:
\$p = array( '£', NULL );
endif;
\$s = \$p[0].number_format( \$f, \$dec ).\$p[1];
}

\$amountAwarded = \$current_records['amountAwarded'];

if (\$amountAwarded == 0)
{
convert_to_money(\$amountAwarded,2);
}
else
{
convert_to_money(\$amountAwarded,0);
}
print (\$amountAwarded);

From using the function tidy_money() i'm losing my decimal point and the currency value is changed.  Is their an easier way of doing all this or can you guide my using this method

Thanks

Ross(newbie)

Let me know if i need to submit this via PHP.  Thanks

0

LVL 13

Expert Comment

ID: 12506069
Why are you asking this in the Javascript section?
0

LVL 31

Expert Comment

ID: 12510035
rossh1977,

JS questions => JS TA
PHP questions => PHP TA

??!?!????!?

:-)

However you should be doing date conversion client side, the will above will do it.

0

Author Comment

ID: 12513823
Yeah no bother, Cheers for the help.  Hop you don't mind i've split the points.

Thanks again

Ross
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
In this blog, we’ll look at how improvements to Percona XtraDB Cluster improved IST performance.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
###### Suggested Courses
Course of the Month16 days, 10 hours left to enroll