简体   繁体   English

HTML 输入模式验证

[英]HTML input pattern validation

 <form class="user" action="code_user.php" method="POST"> <div class="form-group"> <label>Enter Full Name</label> <input class="form-control" pattern="^[a-zA-Z]+(( )+[a-zA-z]+)*$" oninvalid="setCustomValidity('Please enter in alphabets only. ')" type="text" name="name" autocomplete="off" required /> </div> <button type="submit" id="submit"name="signup_btn"class="btn btn-primary btn-user btn-block"> Sign Up </button> </form>

In HTML input validation, i have set the pattern of alphebetic only.在 HTML 输入验证中,我只设置了字母的模式。 When testing with numeric input in it, it shows在其中使用数字输入进行测试时,它显示

However, if i correct the input into the alphabets,it will shows invalid again但是,如果我将输入更正为字母,它将再次显示无效在此处输入图像描述

I have to refresh the page again to input the correct format only it will not show invalid.我必须再次刷新页面才能输入正确的格式,但它不会显示无效。 Is this normal?这是正常的吗? This is my code:这是我的代码:

<div class="form-group">
     <label>Enter Full Name</label>
     <input class="form-control" pattern="^[a-zA-Z]+(( )+[a-zA-z]+)*$" oninvalid="setCustomValidity('Please enter in alphabets only. ')" type="text" name="name" autocomplete="off" required />
</div>

You don't clear your setCustomValidity() .您没有清除setCustomValidity() If you set a value with setCustomValidity() then the field is invalid.如果您使用setCustomValidity()设置值,则该字段无效。 Just add such attribute to your input tag:只需将此类属性添加到您的输入标签:

oninput="setCustomValidity('')"

Fixed in your code:在您的代码中修复:

 <form class="user" action="code_user.php" method="POST"> <div class="form-group"> <label>Enter Full Name</label> <input class="form-control" pattern="^[a-zA-Z]+(( )+[a-zA-z]+)*$" oninvalid="setCustomValidity('Please enter in alphabets only. ')" oninput="setCustomValidity('')" type="text" name="name" autocomplete="off" required /> </div> <button type="submit" id="submit"name="signup_btn"class="btn btn-primary btn-user btn-block"> Sign Up </button> </form>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM