Results 1 to 5 of 5

Thread: Validating number

  1. #1

    Thread Starter
    Frenzied Member I_Love_My_Vans's Avatar
    Join Date
    Jan 2005
    Location
    In the PHP compiler
    Posts
    1,275

    Validating number

    I need to validate a number.

    nnn.nn.nn

    Rules:

    Three sections
    Each section can contain only numbers
    Each section can have any amount of numbers
    Each section is seperated by a .


    Now i have seen some type of validation, but im not sure how they string together?

    Is there any assistance i can get?

    ILMV

  2. #2
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: Validating number

    You should use a regular expression:
    PHP Code:
    $regexp "/([0-9]+)\.([0-9]+)\.([0-9]+)/";

    if (
    preg_match($regexp,$string,$matches)) {
        
    /* matches is now an array contiaing each component*/
        
    print_r($matches);


    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  3. #3
    Don't Panic! Ecniv's Avatar
    Join Date
    Nov 2000
    Location
    Amsterdam...
    Posts
    5,343

    Re: Validating number

    Can't you split a string entered by a character (I recall - haven't done any php for 3 wks .. might try tonight).
    That creates an array - then you check that all the array elements are numeric.
    If not - fail it ...
    If so - continue on...

    Different way

    BOFH Now, BOFH Past, Information on duplicates

    Feeling like a fly on the inside of a closed window (Thunk!)
    If I post a lot, it is because I am bored at work! ;D Or stuck...
    * Anything I post can be only my opinion. Advice etc is up to you to persue...

  4. #4
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Validating number

    Probably. But remember PHP is a interpreted scripting language. The more you can make the PHP engine do with less code, the better. Efficiency is important on any website, and crucial on high-traffic sites.

  5. #5
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: Validating number

    There is a slight over head associated with regular expressions as PHP needs to compile them before executing. This makes the code about 30% slow than loading the string into an array and manually checking each component.

    However, once a regular expression has been compiled by the script, it is then cached. When the cached regular expression is used, it is about 60% quicker.

    Have a look below at the three tests:

    http://adam.codedv.com/examples/speed/speed_test.php

    If you run PHP as a module, the compiled regular expressions are shared among scripts. If you are running PHP as a CGI, each expression must be compiled once for each script execution.
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width