【问题标题】:How to use the attribute "title" without relying on "pattern"?如何在不依赖“模式”的情况下使用属性“标题”?
【发布时间】:2021-03-28 13:32:46
【问题描述】:

所以我是 HTML/PHP 编程的初学者。我需要创建一个捕获错误的基本表单(用户名/密码/电子邮件),并且我发现 HTML 属性“title”在不符合模式时可以很好地显示它们。

但是,我认为我不能创建一个在确认密码时捕获错误的模式(这个想法是强制用户输入两次相同的密码)。

我的目标是在捕获错误时显示类似这样的内容,即使没有任何模式被侵犯

这是我的代码,以备不时之需:

<input type="password" name="pw" size="16" value="<?=$infosVal[3]?>" placeholder="Password" minlength="6">
<input type="password" name="repeat" size="16" value="<?=$infosVal[4]?>" placeholder="Repeat" minlength="6">

有什么办法可以做到吗?谢谢

【问题讨论】:

  • 我不认为有一个仅用于检查两个字段是否相同的 HTML 解决方案。您可以使用 javascript(客户端)或 PHP(服务器端)进行确认检查
  • 这不是我的问题。我知道我需要 PHP 来捕获错误,我在这里的目标是以好看的方式显示错误,所以使用属性标题。如果我不使用属性标题,我可以显示像this 这样看起来很糟糕的消息,但结果会很奇怪

标签: php html input attributes title


【解决方案1】:
<form method='post'>
    <input type="password" placeholder="Password" name='pass1' oninput="document.querySelector('#pass2').pattern=this.value" required=""> <br>
    <input type="password" placeholder="Confirm Password" id='pass2' name='pass2'
     pattern="" title="The second password does not match the first!" required="" /><br>
     <input type="submit" name="check">
</form>
<?php
    if(isset($_POST["check"])){
        $password = $_POST["pass1"];
        $confirmedPassword = $_POST["pass2"];
        if($password === $confirmedPassword)
            print('login success!');
    }
?>

【讨论】:

  • 感谢您的回答,但我只需要这样做 HTML/PHP。
  • 我更新了答案以适合您仅使用 HTML/PHP 执行的操作。检查一下。
  • 虽然这看起来很明显,但它对特殊字符非常有效。我用 preg_quote() 让它工作
  • 我不会尝试使用特殊字符,如果您可以与我们分享您如何使其与preg_quote() 一起使用?
猜你喜欢
  • 2011-01-03
  • 1970-01-01
  • 1970-01-01
  • 2016-03-31
  • 1970-01-01
  • 2011-09-02
  • 1970-01-01
  • 2016-07-09
相关资源
最近更新 更多