How can I easily validate the name input on the form using bootstrap or php? I want this field to reject all numbers 0-9. Can you please help me out?
Here is the field, which is a part of the form I use:
<input class="form-control" id="name" placeholder="NAME" type="text" name="name">
And this is the php script which sends the form:
<?php
$adresdo = "info@takelake.com";
$temat = "Newsletter signup";
$zawartosc = "Imie: ".$_POST['name']."
"
."Email: ".$_POST['email']."
"
."Selected".implode($_POST['checkbox'],",");
// if the url field is empty
if(isset($_POST['url']) && $_POST['url'] == ''){
if(!$_POST['name'] || !$_POST['email']){
header("Location: error.html");
exit;
}
$email = $_POST['email'];
if(mail($adresdo, $temat, $zawartosc, 'From: Subskrybent <'.$email.'>'))
{
header("Location: ok.html");
}
}
header("Location: error.html");
?>
you can use html5 code for this and this will allow space and if you want to check at least 2 character then use [a-zA-Z][a-zA-Z ]{2,}
or use [a-zA-Z][a-zA-Z ]{1,}
<input type="text" name="name" pattern="[a-zA-Z][a-zA-Z ]{2,}" required>
and this not allow space
<input type="text" name="name" pattern="[a-zA-Z]{1,}" required>
for more information
https://www.w3schools.com/tags/att_input_pattern.asp
and this is php validation
<?php
$adresdo = "info@takelake.com";
$temat = "Newsletter signup";
$zawartosc = "Imie: " . $_POST['name'] . "
"
. "Email: " . $_POST['email'] . "
"
. "Selected" . implode($_POST['checkbox'], ",");
// if the url field is empty
if (isset($_POST['url']) && $_POST['url'] == '') {
// then send the form to your email
mail('you@yoursite.com', 'Contact Form', print_r($_POST, true));
if (!$_POST['name'] || !$_POST['email']) {
header("Location: error.html");
}
if (preg_match('~[0-9]~', $_POST['name'])) {
header("Location: error.html");
exit();
}
$email = $_POST['email'];
if (mail($adresdo, $temat, $zawartosc, 'From: Subskrybent <' . $email . '>')) {
header("Location: ok.html");
}
}
Use following code to check if it contains number:
if(preg_match('~[0-9]~', $_POST['name'])) {
//has numbers
} else {
//no numbers
}
Updated your code:
<?php
$adresdo = "info@takelake.com";
$temat = "Newsletter signup";
$zawartosc = "Imie: ".$_POST['name']."
"
."Email: ".$_POST['email']."
"
."Selected".implode($_POST['checkbox'],",");
// if the url field is empty
if(isset($_POST['url']) && $_POST['url'] == ''){
// then send the form to your email
mail( 'you@yoursite.com', 'Contact Form', print_r($_POST,true) );
if(!$_POST['name'] || !$_POST['email']){
header("Location: error.html");
exit;
if(preg_match('~[0-9]~', $_POST['name'])) {
header("Location: error.html");
exit();
}
}
$email = $_POST['email'];
if(mail($adresdo, $temat, $zawartosc, 'From: Subskrybent <'.$email.'>'))
{
header("Location: ok.html");
exit();
}
}
header("Location: error.html");
exit();
?>
To check if a string has numbers in PHP, regex is the way to go:
if (preg_match('/[0-9]+/', $_POST['field_name'])) {
// add logic if the POST value has numbers in it...
}
Or wrap it in a function if you want to reuse it for other operations:
function containsNumbers($string) {
return preg_match('/[0-9]+/', $string) > 0;
}
Use like:
if (containsNumbers($_POST['field_name'])) {
// logic here
}
Don't rely on front-end validation only. That's what the back-end is for. HTML input patterns and javascript validation can be easily circumvented. The best practice would be to check on the front-end via Javascript for better UI response, but to re-check in the back-end via PHP in case the user attempts to circumvent the rules.
Here's a checker in Javascript:
var checkIsValidName = function(string) {
// returns false if string contains numbers
var pattern = /[^0-9]+/;
// returns false if string contains chars other than A-Z, a-z, space, apostraphe and hyphen
// also returns false on accented characters, as this checks the ASCII values
var pattern = /[A-Za-z\s\'-]/;
return string.match(pattern);
}
Usage:
if (checkIsValidName()) {
// javascript logic here
alert('Not Valid Name');
}
This regex pattern will match all letters, spaces, hyphens, apostraphes and standard accented characters (western european):
[A-Za-z\s\'-ßàáâãäåæçèéêëìíîïðñòóôõöøùúûüý]
You see this can get messy quick, and it will reject the string if it contains any non-ASCII letters we don't include, like Cyrillic, Polish and any Asian alphabet character.
Because if this, it's best to be exclusive and not inclusive with a regex check, because there will always be more characters you will allow rather than ones you disallow. This of course, refers to people's NAMES, and not passwords or email addresses, where you want to use an inclusive pattern (since there is a limit to the characters you can actually use).
Note: although useful and seemingly simple, using the HTML5 pattern
attribute in an input tag is not secure, as this is not supported in IE9 and Safari.
It's best to check via javascript for a quick responsive UI that than thwart most bad input, but you always should let the PHP back-end handle the final validation - making sure that the user's input is kept, so they don't have to enter the information more than once, save for editing it.
Use AJAX with JavaScript and PHP for best results for your data and the user's experience.
pattern="[A-Za-z]" Its default Attributes of html<input class="form-control" id="name" placeholder="NAME" type="text" pattern="^\D*$" name="name" title="Number Not Allowed">