www.webdeveloper.com
Results 1 to 12 of 12

Thread: Number only validation

  1. #1
    Join Date
    Jul 2011
    Location
    Irish Republic
    Posts
    64

    Number only validation

    I am trying to get the preg_match '/[^0-9]/' thing below to validate or clear number only entries into a phone field. Am I on the right track as currently I cannot get it to work?

    PHP Code:
    public function validatePhone($phoneVal$phoneName) {
            if (
    strlen($phoneVal) <= 0) {
                
    $this->setError($phoneName"Phone Number Required");
            } else if (!
    preg_match('/[^0-9]/''',$phoneVal)) {
                
    $this->setError($phoneName"Valid Phone Number Required");
            }
        } 
    PHP Code:
    <p><label>Phone: <span class="required">Required</span>
              <input type="text" name="phone" class="textfield" value="<?php echo htmlentities($phone); ?>" />
              </label><span class="errors"><?php echo $phoneErr ?></span></p>

    Thanks in advance for your oversight.

  2. #2
    Join Date
    May 2004
    Location
    chennai, tamil nadu, India
    Posts
    448
    Try

    } else if (preg_match('/[^0-9]/', $phoneVal)) {
    Chris, Senior Developer,
    Php laravel developers,
    www.chrisranjana.com

  3. #3
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,387
    If you just want to validate that a string contains only digits:
    PHP Code:
    if(ctype_digit($string)) {
      
    // good
    } else {
      
    // bad

    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  4. #4
    Join Date
    Jul 2011
    Location
    Irish Republic
    Posts
    64
    Thanks guys, that has resolved the issue. I discovered that

    } else if (!is_numeric ($phoneVal)) {

    also does the job.

    In the circumstances which is the better, the !is_numeric or the preg_match approach ?

    Also, what impact does the ! have in/out such code ?

  5. #5
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,387
    "!" is essentially the same as "false ==". It inverts the Boolean result of what it precedes, so if is_numeric() returns false, then ! is_numeric() returns true; while if is_numeric() returns true, then ! is_numeric() returns false.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  6. #6
    Join Date
    Jul 2011
    Location
    Irish Republic
    Posts
    64
    Thanks for the lesson in opposites. Great answer.

  7. #7
    Join Date
    May 2004
    Location
    chennai, tamil nadu, India
    Posts
    448
    http://php.net/manual/en/function.is-numeric.php

    ........ +0123.45e6 is a valid numeric value. Hexadecimal notation (0xFF) is allowed too ......
    so not sure whether is_numeric is sufficient for validating phone numbers containing only numbers, I would go with preg_match.
    Chris, Senior Developer,
    Php laravel developers,
    www.chrisranjana.com

  8. #8
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,387
    Quote Originally Posted by chrisranjana View Post
    http://php.net/manual/en/function.is-numeric.php



    so not sure whether is_numeric is sufficient for validating phone numbers containing only numbers, I would go with preg_match.
    Or ctype_digit(), which will be more efficient if that's all you need to test for.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  9. #9
    Join Date
    Mar 2012
    Posts
    63
    Hi

    is_numeric is not work, because it gives true for "6.5" too

    so its best with preg_match only

    Let me know if any issues

    Thanks
    DS

  10. #10
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,387
    Quote Originally Posted by masterwin View Post
    Hi

    is_numeric is not work, because it gives true for "6.5" too

    so its best with preg_match only

    Let me know if any issues

    Thanks
    DS
    Or ctype_digit(), which will be more efficient if that's all you need to test for.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  11. #11
    Join Date
    May 2004
    Location
    chennai, tamil nadu, India
    Posts
    448
    Deleted
    Last edited by chrisranjana; 06-22-2012 at 10:40 AM. Reason: deleting this post as it contains "alpha" (http://stackoverflow.com/questions/1936504/php-regex-vs-ctype)
    Chris, Senior Developer,
    Php laravel developers,
    www.chrisranjana.com

  12. #12
    Join Date
    May 2004
    Location
    chennai, tamil nadu, India
    Posts
    448
    Quote Originally Posted by NogDog View Post
    "!" is essentially the same as "false ==". It inverts the Boolean result of what it precedes, so if is_numeric() returns false, then ! is_numeric() returns true; while if is_numeric() returns true, then ! is_numeric() returns false.
    Good ctype is good. I mean "late realization is always good"
    Last edited by chrisranjana; 06-22-2012 at 10:08 AM. Reason: edited
    Chris, Senior Developer,
    Php laravel developers,
    www.chrisranjana.com

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

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



Recent Articles