SitePoint Sponsor

User Tag List

Results 1 to 6 of 6

Thread: Post Validation

  1. #1
    SitePoint Zealot txt3rob's Avatar
    Join Date
    Jul 2013
    Location
    Liverpool UK
    Posts
    171
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Post Validation

    I have a website that when you submit two numbers i need to ensure that they both start correctly

    how can i make it so if the first field say "from" must start with 07 and the to field must start 447 if one or both are incorrect i can echo a error message.

    i know it will be an if statement but not sure how to do it as i need to make sure that when the details are posted they are correctly formatted.

  2. #2
    SitePoint Zealot txt3rob's Avatar
    Join Date
    Jul 2013
    Location
    Liverpool UK
    Posts
    171
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    im guessing it might be something like

    PHP Code:
    // Check if numbers are correct

    if (preg_match('/^44/'$from)) {

    }

    else if (
    preg_match('/^07/'$to)) {


    what i would like it to do tho is if it's true continue with the rest of the script but if it's false then echo and error message

  3. #3
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    4,826
    Mentioned
    142 Post(s)
    Tagged
    0 Thread(s)
    Yes, you are close:
    PHP Code:
    if (!preg_match('/^44/'$to) || !preg_match('/^07/'$from))
    {
      die(
    'you failed the validation');
    }

    // Hey it passed if it gets down to here! 
    You can also use an else statement
    PHP Code:
    if (!preg_match('/^447/'$to) || !preg_match('/^07/'$from))
    {
      
    // echo "You failed the validation";
    }
    else
    {
      
    // echo "You passed the validation";

    Be sure to congratulate xMog on earning April's Member of the Month
    Go ahead and blame me, I still won't lose any sleep over it
    My Blog | My Technical Notes

  4. #4
    SitePoint Zealot txt3rob's Avatar
    Join Date
    Jul 2013
    Location
    Liverpool UK
    Posts
    171
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    cpradio once again a big thank you for your help.

    i have tested it and it does echo what i want but how i can stop the script from continuing cause when i tested this it did echo what i wanted but continued with the script.

  5. #5
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    4,826
    Mentioned
    142 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by txt3rob View Post
    cpradio once again a big thank you for your help.

    i have tested it and it does echo what i want but how i can stop the script from continuing cause when i tested this it did echo what i wanted but continued with the script.
    Are you able to post more of your script so I can see what context you used it in?
    Be sure to congratulate xMog on earning April's Member of the Month
    Go ahead and blame me, I still won't lose any sleep over it
    My Blog | My Technical Notes

  6. #6
    SitePoint Zealot txt3rob's Avatar
    Join Date
    Jul 2013
    Location
    Liverpool UK
    Posts
    171
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    just threw
    PHP Code:
    exit(): 
    in to the if false part

    so now reads

    PHP Code:
    // Check if the form has been inputted correctly
    if (!preg_match('/^447/'$to) || !preg_match('/^07/'$from))
    {
       echo 
    "Please ensure you have entered the numbers correct";
       exit();
    }
    else
    {
      
    // Continue



Bookmarks

Posting Permissions

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