How to check this valus against mysql

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mmarif4u
    New Member
    • Sep 2006
    • 23

    How to check this valus against mysql

    Hi everybody...

    I want to enter values to db like the following,,,
    Format is like this (810605-14-6356)
    This is the rite format, No a to z letters...
    6 digits then - then 2 digits then - then 4 digits.

    Code:
     dbConnect('db');
        
       $ic = $_POST['icnumber'];
       
       if (preg_match('#^[0-9]{6}-[0-9]{2}-[0-9]{4}$#', $ic)) { 
        //$ic is valid
        error ('IC Number is not in right format.\\n'.
                  'Please enter IC Number agian.');
         }
       
       if ($ic==''
          or $_POST['cicnumber']=='' )  { 
             error ('One or more required fields were left blank.\\n'.
                  'Please fill them in and try again.');
        }
        // this makes sure both passwords entered match
       if ($ic != $_POST['cicnumber']) {
       error ('Your IC Numbers not matched.\\n'.
                  'Please try again.');
       }
         
        // Check for existing user with the ic number
        $sql = "SELECT COUNT(*) FROM m_users WHERE icnumber = $ic ";
        $result = mysql_query($sql);
        if (!$result) {	
            error('A database error occurred in processing your '.
                  'submission.\\nIf this error persists, please '.
                  'contact you@example.com.');
        }
         
        if (mysql_result($result,0,0)>0) {
            error('A user already exists with your chosen IC Number.\\n'.
                  'Please try your own IC Number.');
        }
        
            
        $random = rand(1000000,9999999990);  
        
        
        $sql = "INSERT INTO m_users SET
                  icnumber = ('$ic'),
                  acccode = ('$random'),
                  actdate = curdate(),
                  expdate = DATE_ADD(curdate(), INTERVAL 1 month);
                  ";
        if (!mysql_query($sql))
            error('A database error occurred in processing your '.
                  'submission.\\nIf this error persists, please '.
                  'contact admin@straight-a.com.my .\\n' . mysql_error());
                
        ?>
    I make this code for it,, but its not working can anyone help me...

    Code:
    $ic = $_POST['icnumber'];
       
       if (preg_match('#^[0-9]{6}-[0-9]{2}-[0-9]{4}$#', $ic)) { 
        //$ic is valid
        error ('IC Number is not in right format.\\n'.
                  'Please enter IC Number agian.');
         }
    Thanks in advance.
  • cassbiz
    New Member
    • Oct 2006
    • 202

    #2
    Have you thought about creating three different fields in your MySQL table then in regards to the form separate it with three different fields. This way it will be able to read all the numbers simply without complicated code.

    Comment

    • ronverdonk
      Recognized Expert Specialist
      • Jul 2006
      • 4259

      #3
      Special chars, when taken literally, must be escaped. You must escape the hyphens in your pattern with a backslash. Like
      Code:
      [0-9]{6}\-[0-9]{2}\-[0-9]{4}
      Ronald :cool:

      Comment

      • mmarif4u
        New Member
        • Sep 2006
        • 23

        #4
        Thanks Ronald the problem is not solved with that.
        i dont know why....
        Plz help me out of this prob.

        Thanks...

        Comment

        • ronverdonk
          Recognized Expert Specialist
          • Jul 2006
          • 4259

          #5
          I did not show the full expression, just a sample of using literals. Here is the full test[php]if (preg_match('^\ d{6}\-\d{2}\-\d{4}$^', $test)) {
          echo "Matched on $test<br />";
          }
          else {
          echo "Failed match on $test<br />";
          }[/php]
          Ronald :cool:

          Comment

          • mmarif4u
            New Member
            • Sep 2006
            • 23

            #6
            Thanks Ronald For reply have a great 2007.

            I am now far away From my PC.
            i will test it later..
            But u use \d except of [0-9] ,, it will work..
            I dont want to echo if the icnumber format is correct but if
            wrong then error(i have common.php in which i call error mag), not echo..

            Thanks again for ur help..

            Comment

            • ronverdonk
              Recognized Expert Specialist
              • Jul 2006
              • 4259

              #7
              It is just a working sample to show you how to test it.You can adapt it to your heart's delight.

              Ronald :cool:

              Comment

              • mmarif4u
                New Member
                • Sep 2006
                • 23

                #8
                Thanks Ronald for help..

                I will test this, if any problem i will post reply...

                Thanks

                Comment

                • mmarif4u
                  New Member
                  • Sep 2006
                  • 23

                  #9
                  Thanks Ronlad for ur help.
                  i make some changes in it.. and now it is working..

                  Code:
                  if (preg_match('#^[0-9]{6}-[0-9]{2}-[0-9]{4}$#', $ic)) { 
                     
                      }
                      else {      
                      error('IC Number format is not Valid.\\n'.
                                'Please try again.');
                         }
                  Thanks again for ur kind work on this prob...

                  Comment

                  Working...