Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Making sure user enters only numbers
Message
Information générale
Forum:
Internet
Catégorie:
VBScript
Divers
Thread ID:
00427000
Message ID:
00427459
Vues:
16
Thanks Mike the "JScriptman"! :)

>HTH: Here are a couple of JScript function you can try depending on what you want to do when a non-numeric is entered:
>
>/******************************************************************************
>** This function pulls out all numberic characters from a text string
>** (as well as the sign and decimal). The return value is then suitable
>** for use in the "parseInt" and "parseFloat" functions.
>** The function STOPS parsing once a non-numeric character is found
>** (with the exceptions of the dollar sign, plus sign, comma, and space)
>******************************************************************************/
>function ToNumeric(sText)
>{
> var i, iStart, iEnd, iSign;
> var cChar, sNumbers;
>
> iEnd = sText.length - 1;
> iSign = 1;
> sNumbers = '';
>
> if (iEnd == -1)
> {
> return '0';
> }
>
> for (i=0; i<=iEnd; i++)
> {
> cChar = sText.charAt(i);
>
> switch (cChar)
> {
> case '0':
> case '1':
> case '2':
> case '3':
> case '4':
> case '5':
> case '6':
> case '7':
> case '8':
> case '9':
> case '.':
> sNumbers += cChar;
> break;
>
> case '$':
> case ',':
> case '+':
> case ' ':
> break;
>
> case '-':
> iSign *= -1;
> break;
>
> default:
> i = iEnd;
> break;
> }
> }
>
> if (sNumbers == '') sNumbers = '0';
>
> return (iSign == -1 ? "-" : "") + sNumbers;
>}
>
>/******************************************************************************
>** A simple function to encapsulate the parseInt/ToNumeric functions.
>******************************************************************************/
>function CInt(sText)
>{
> return parseInt(ToNumeric(sText));
>}
>
>/******************************************************************************
>** A simple function to encapsulate the parseFloat/ToNumeric functions.
>******************************************************************************/
>function CSng(sText)
>{
> return parseFloat(ToNumeric(sText));
>}
>
>/******************************************************************************
>** Performs a close approximation to the VB "IsNumeric" function.
>******************************************************************************/
>function IsNumeric(sValue)
>{
> var i, x, sgn = 0, InDigits = false;
> var sPattern = " 0123456789.,$+-";
> var sText = Trim(sValue);
> var iEnd = sText.length - 1;
>
> // Empty strings are not numeric.
> if (iEnd == -1) return false;
>
> for (i=0; i<=iEnd; i++)
> {
> x = sPattern.indexOf(sText.charAt(i));
>
> // If it's not in the pattern, we're done.
> if (x == -1) return false;
>
> // Set a flag the first time we encounter a digit.
> if (x > 0 && x < 11) InDigits = true;
>
> // If we find a space after the numbers, then we quit.
> if (x == 0 && InDigits) return false;
>
> // We're done if we find more than one "-" or "+".
> if (x > 13)
> {
> sgn++;
> if (sgn > 1) return false;
> }
> }
>
> return true;
>}
It's "my" world. You're just living in it.
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform