Re: RES: input field validation

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Ok...so, I'm a couple days behind...

[somewhat OT tidbit]
!isNaN(null) will also return true since null is parsed as 0.

Now, if you're going to use a String search, might as well use something
built-in that does just that.
First advantage: shorter code and smaller files.
Also, RegExp's usually (but, no always) parse faster than charAt loops.

Javascript:

  function isInteger(value) {
    // accepts untrimmed strings
    return (/^\s*[0-9]+\s*$/).test(String(value));
  }

  function findInteger(value) {
    return String(value).replace(/^[^0-9]*([0-9]*).*?$/, '$1');
  }

Tests:

  var i, j;
  var funcs = [isInteger, findInteger];
  var input = ['12 ', '12sds', 'fhe34', 'a12.3b', '1abc2', 99, 2.4, null];
  for (i = 0; i < funcs.length; i += 1) {
    for (j = 0; j < input.length; j += 1) {
      document.write(funcs[i](input[j]), ';  ');
    }
    document.write('<br>');
  }

Output:

  true; false; false; false; false; true; false; false;
  12; 12; 34; 12; 1; 99; 2; ;

Usage:

  <input type="text" onblur="this.value=findInteger(this.value);" /><!--
silently strips invalid characters -->
  <span>(integers only)</span>

  function validate() {
    if (!isInteger(FormObject.input1.value)) {
      alert('Please enter an integer for ...');
    }
  }

- Jon L.

On Mon, May 12, 2008 at 11:47 AM, YVES SUCAET <yves.sucaet@xxxxxxx> wrote:

> Nope, it's not. The problem was formulated as "Integer" detection. not
> numerical detection in general. isNaN will return positive for e.g.
> floating
> point values as well. The solutions described in this thread all result in
> integer-only values.
>
> All solutions presented are still off-topic, of course...
>
> Yves
>
> ------ Original Message ------
> Received: Mon, 12 May 2008 11:12:02 AM CDT
> From: "Thiago Pojda" <thiago.pojda@xxxxxxxxxxxxxxxxxx>
> To: "'sahabettin akca'" <adminsaho@xxxxxxxxx>,  <php-db@xxxxxxxxxxxxx>
> Subject: RES: [PHP-DB] input field validation
>
> It'd be simpler to use isNaN()
>
>
>
> Thiago Henrique Pojda
> Desenvolvimento Web
> +55 41 3033-7676
> thiago.pojda@xxxxxxxxxxxxxxxxxx
> Excelência em Softwares Financeiros
>
>
> -----Mensagem original-----
> De: sahabettin akca [mailto:adminsaho@xxxxxxxxx]
> Enviada em: domingo, 11 de maio de 2008 14:31
> Para: php-db@xxxxxxxxxxxxx
> Assunto: Re: [PHP-DB] input field validation
>
> hi,
>
> *javascript function :
>
> function isNumber(obj) {
>        var len  = obj.value.length;
>        var lastChar = obj.value.charAt(len-1);
>        if( lastChar != '0' && lastChar != '1' && lastChar != '2' &&
> lastChar
> != '3' && lastChar != '4' &&
>
>
>        lastChar != '5' && lastChar != '6' && lastChar != '7' && lastChar !=
> '8' && lastChar != '9' ) {
>                obj.value = obj.value.substring(0, len-1);
>
>        }
>
> }
>
> this is function use :
> <input *onKeyUp='isNumber(this)' .... />
>
> do not alert , but add alert
> ----
> *               obj.value = obj.value.substring(0, len-1);
> ---
> this line before
>
> ---
> alert('this field only integer!');
> *
>
> ----
>
> add.
> good luck
> ?ahabettin akca (saho)
> 2008/5/11 arafat uddin <arafat.cse@xxxxxxxxx>:
> -------------------------
> and
> this database field data type select int (integer) ,
> tinyint, int, bigint... or
> if(gettype($_POST['field'])!="integer") // or
> if(intval($_POST['field'])==0)
> or if(!intval($_POST['field']))
> {
> print"not integer";
> location("header: xxxx.php");
> #or
> print"<meta http-equiv='refresh' content='0;url=xxxx.php'>";
> }
> else
> {
> ...
> }
> 2008/5/11 Yves Sucaet <yves.sucaet@xxxxxxx>:
>
> > Allow me to point out that this is not database-related...
> >
> > This website should get you everything you want:
> > http://www.acmesoffware.com/acme/ExamplesJS/jsExm_ValidateInteger.asp
> >
> > HTH,
> >
> > Yves
> >
> > ----- Original Message ----- From: "arafat uddin" <arafat.cse@xxxxxxxxx>
> > To: <php-db@xxxxxxxxxxxxx>
> > Sent: Sunday, May 11, 2008 12:48 AM
> > Subject: [PHP-DB] input field validation
> >
> >
> >
> >  hi,
> > >  i want to input only integer in my field.if without integer any
> > > input put in the field it will show warning messege.
> > > such as "12 "will be accept
> > > but "12sds" will not accept .
> > > "fhe34" will not accept
> > >
> > >
> > > plz help me by any javascript code.
> > >
> > > --
> > > PHP Database Mailing List (http://www.php.net/)
> > > To unsubscribe, visit: http://www.php.net/unsub.php
> > >
> > >
> > >
> >
> > --
> > PHP Database Mailing List (http://www.php.net/)
> > To unsubscribe, visit: http://www.php.net/unsub.php
> >
> >
>
>
> --
> ?ahabettin akca // saho
>
>
>
> --
> PHP Database Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>
>
>
>
>
> --
> PHP Database Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>

[Index of Archives]     [PHP Home]     [PHP Users]     [Postgresql Discussion]     [Kernel Newbies]     [Postgresql]     [Yosemite News]

  Powered by Linux