www.webdeveloper.com
Results 1 to 7 of 7

Thread: Validate String (Regular Expression)

  1. #1
    Join Date
    Aug 2013
    Posts
    31

    Validate String (Regular Expression)

    I working on the form where i want to restrict the user to input only characters,digit, . , and white space. i want to validate 2 text field and one text area.

    HTML Code:
    if(!preg_match("/^[a-zA-Z0-9].*$/",$field1))
                {
                    $turnmsg = "<b><font color='red'> Only letters,spaces & .  are allowed </font></b>";
                }
                
                if(!preg_match("/^[a-z A-Z].*$/",$field2))
                {
                    $turnmsg = "<b><font color='red'> Only letters,spaces & .  are allowed </font></b>";
                }
                
                if(!preg_match("/^[a-z A-Z].*$/",$textarea1))
                {
                    $turnmsg = "<b><font color='red'> Only letters,spaces & .  are allowed </font></b>";
                }

    The problem is that when i applied preg_match on single field with if condition its working fine , but with the mutiple if condition its fail. any alternatives or do i needs any modificaiton into my code.



    Thanks ,

    Ashish

  2. #2
    Join Date
    Dec 2011
    Location
    Centurion, South Africa
    Posts
    776
    Modified your expression a little, and seeing that you require it on all three fields I'm appending them together:

    Code:
    $turnmsg = '';
    
    if (!preg_match('/^[a-zA-Z\d\s\.]*$/', $field1 . $field2 . $textarea1))
    {
        $turnmsg = "<b><font color='red'> Only letters,spaces & .  are allowed </font></b>";
    }
    
    echo $turnmsg;
    JavaScript: Learn | Validate | Compact

  3. #3
    Join Date
    Aug 2013
    Posts
    31
    Its not working still i can able to enter special characters into my database(%,$#@ etc)

  4. #4
    Join Date
    Dec 2011
    Location
    Centurion, South Africa
    Posts
    776
    Not that I could really see anything wrong with the previous expression, I've changed it a little again anyway.
    Also how do you determine a validation error, or if you need to write to the database?

    Code:
    <?php
    
    $field1    = 'abc.123 ' . "\n\r\t";
    $field2    = '';
    $textarea1 = '%$#@';
    
    $turnmsg = '';
    
    if (preg_match('/[^a-zA-Z\d\s\.]/', $field1 . $field2 . $textarea1)) {
        $turnmsg = "<b><font color='red'> Only letters,spaces & .  are allowed </font></b>";
    } else {
    
    	//WRITE TO DATABASE?
    
    }
    
    echo $turnmsg;
    
    ?>
    The current example outputs:

    Code:
    Only letters,spaces & . are allowed
    JavaScript: Learn | Validate | Compact

  5. #5
    Join Date
    Aug 2013
    Posts
    31
    Thanks for your reply, its working for me now.

  6. #6
    Join Date
    Aug 2013
    Posts
    31
    @ bionoid

    One more question can i use htmlspecialchars or htmlentities so i can avoid my form from hackers
    if yes then could you please give me an example by taking my above code

    Thanks again

  7. #7
    Join Date
    Aug 2013
    Posts
    31
    Is this right way to used htmlspecialchars function

    $field1= htmlspecialchars($_POST['field1']); ?

Thread Information

Users Browsing this Thread

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

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