?
Solved

IsNumeric allow certain characters, £.,

Posted on 2004-11-05
7
Medium Priority
?
415 Views
Last Modified: 2008-02-01
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
Comment
Question by:rossh1977
  • 3
  • 2
  • 2
7 Comments
 
LVL 13

Assisted Solution

by:StormyWaters
StormyWaters earned 1000 total points
ID: 12504152
What an odd function...
Use regexp instead.
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

by:
GwynforWeb earned 1000 total points
ID: 12505411
try this

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

//TRUE VALUES

val='£234234.9800'
alert(val+' '+isNumeric(val))

val='£23420'
alert(val+' '+isNumeric(val))

val='234234.9800£'
alert(val+' '+isNumeric(val))

val='234234.9800'
alert(val+' '+isNumeric(val))


//FALSE VALUE

val='$234234.9800'
alert(val+' '+isNumeric(val))

val='234234.9800$'
alert(val+' '+isNumeric(val))

val='23£420'
alert(val+' '+isNumeric(val))

val='£234234.9800£'
alert(val+' '+isNumeric(val))

val='234234.9800$'
alert(val+' '+isNumeric(val))

val='234234.98ABCD'
alert(val+' '+isNumeric(val))
</script>

0
 
LVL 31

Expert Comment

by:GwynforWeb
ID: 12505430
the above is easily fine tuned for any subcases that I may have wrong
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:rossh1977
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'];
$overheads = $current_records['overheads'];
            
            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

by:StormyWaters
ID: 12506069
Why are you asking this in the Javascript section?
0
 
LVL 31

Expert Comment

by:GwynforWeb
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

by:rossh1977
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

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

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

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question