IsNumeric allow certain characters, £.,

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
rossh1977Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

StormyWatersCommented:
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
GwynforWebCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
GwynforWebCommented:
the above is easily fine tuned for any subcases that I may have wrong
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

rossh1977Author Commented:
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
StormyWatersCommented:
Why are you asking this in the Javascript section?
0
GwynforWebCommented:
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
rossh1977Author Commented:
Yeah no bother, Cheers for the help.  Hop you don't mind i've split the points.

Thanks again

Ross
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.