Search code examples
phpregexif-statementpattern-matchingpreg-match

How is multiple variable control done for preg match inside of if conditional?


I check the inputs that come with the post method. But this if conditional code not work. Edit: Else falls on his block

Is JavaScript

$(".inputonlytext").keypress(function(e) {

    var key = e.keyCode;
    if ((key >= 48 && key <= 57) || (key >= 33 && key <= 47) || (key >= 58 && key <= 64) || (key >= 91 && key <= 96) || (key >= 123 && key <= 127)) {
        e.preventDefault();
    }
    /*if (e.which === 32)
        return false; */
    //var text = $(this).val();
    //$(this).val(text.replace(" ", ""));

});

Is Input

<input type="text" name="name" class="form-control inputonlytext" maxlength="50" required="required">
<input type="text" name="surname" class="form-control inputonlytext" maxlength="50" required="required">

Is PHP;

if($_POST["name"] !== null && preg_match('/^[a-zA-Z ]+$/', $_POST["name"]) && $_POST["surname"] !== null && preg_match('/^[a-zA-Z ]+$/', $_POST["surname"])){
            //true for my application logic. (think like a reverse)
}else{
            //false for my application logic. (think like a reverse)
}

Solution

  • Thank you for your interest.

    Here is an alternative;

    I used

    $_POST["name"] !== null
    

    instead of

    isset($_POST["name"])