HTML5 form validation pattern alphanumeric with spaces?

后端 未结 7 2118
借酒劲吻你
借酒劲吻你 2020-12-08 00:10

I have the following input tag in my html5 form:

相关标签:
7条回答
  • 2020-12-08 00:42

    It's quite an old question, but in case it could be useful for anyone, starting from a combination of good responses found here, I've ended using this pattern:

    pattern="([^\s][A-z0-9À-ž\s]+)"
    

    It will require at least two characters, making sure it does not start with an empty space but allowing spaces between words, and also allowing special characters such as ą, ó, ä, ö.

    0 讨论(0)
  • 2020-12-08 00:44

    Use Like below format code

    $('#title').keypress(function(event){
        //get envent value       
        var inputValue = event.which;
        // check whitespaces only.
        if(inputValue == 32){
            return true;    
        }
         // check number only.
        if(inputValue == 48 || inputValue == 49 || inputValue == 50 || inputValue == 51 || inputValue == 52 || inputValue == 53 ||  inputValue ==  54 ||  inputValue == 55 || inputValue == 56 || inputValue == 57){
            return true;
        }
        // check special char.
        if(!(inputValue >= 65 && inputValue <= 120) && (inputValue != 32 && inputValue != 0)) { 
            event.preventDefault(); 
        }
    })
    
    0 讨论(0)
  • 2020-12-08 00:44

    Use below code for HTML5 validation pattern alphanumeric without / with space :-

    for HTML5 validation pattern alphanumeric without space :- onkeypress="return event.charCode >= 48 && event.charCode <= 57 || event.charCode >= 97 && event.charCode <= 122 || event.charCode >= 65 && event.charCode <= 90"

    for HTML5 validation pattern alphanumeric with space :-

    onkeypress="return event.charCode >= 48 && event.charCode <= 57 || event.charCode >= 97 && event.charCode <= 122 || event.charCode >= 65 && event.charCode <= 90 || event.charCode == 32"

    0 讨论(0)
  • 2020-12-08 00:46

    Use this code to ensure the user doesn't just enter spaces but a valid name:

    pattern="[a-zA-Z][a-zA-Z0-9\s]*"
    
    0 讨论(0)
  • 2020-12-08 00:48

    How about adding a space in the pattern attribute like pattern="[a-zA-Z0-9 ]+". If you want to support any kind of space try pattern="[a-zA-Z0-9\s]+"

    0 讨论(0)
  • 2020-12-08 00:58

    My solution is to cover all the range of diacritics:

    ([A-z0-9À-ž\s]){2,}
    

    A-z - this is for all latin characters

    0-9 - this is for all digits

    À-ž - this is for all diacritics

    \s - this is for spaces

    {2,} - string needs to be at least 2 characters long

    0 讨论(0)
提交回复
热议问题