dontoften8899 2017-04-17 09:35
浏览 32
已采纳

如何验证名称字段以避免数字

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");
  ?>
  • 写回答

4条回答 默认 最新

  • douying0108 2017-04-17 09:49
    关注

    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");
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 WPF 大屏看板表格背景图片设置
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示