HTML simple not blank pattern

后端 未结 2 1624
無奈伤痛
無奈伤痛 2020-12-29 03:52

I have a simple form and i want the submit button not to work for the conditions i give in the pattern, but if i leave it blank the submit works. how can i make the pattern

相关标签:
2条回答
  • 2020-12-29 04:38

    HTML has the required attribute to accomplish this. If you set any input to be required, modern browsers won't let you submit the form if those fields are empty.

    <input type="text" name="username" required="required" pattern="[A-Za-z0-9]{1,20}">
    
    0 讨论(0)
  • 2020-12-29 04:42

    To prevent errors from showing on load, you can not use the HTML5 required attribute. You can use JavaScript. For example:

    if ( $('#form-password').val() === "" ) 
    {
        e.preventDefault();
    }
    

    Using HTML Patterns to match at least one:

    <input type="text" name="username" pattern=".{1,}">
    
    0 讨论(0)
提交回复
热议问题