Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex allow letters and space only in html input box

Tags:

html

regex

I want to allow only letters and space in input box like "john deo". but the above doesn't work with below Regex and it is not allowing space.

<!DOCTYPE html">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Untitled Document</title>
</head>

<body>
<label>full name
<input type="text" name="textfield" onkeydown="return /[a-z]/i.test(event.key)"
    onblur="if (this.value == '') {this.value = '';}"
    onfocus="if (this.value == '') {this.value = '';}"/>
</label>
</body>
</html>
like image 361
FredBrown Avatar asked Oct 27 '25 06:10

FredBrown


1 Answers

change your regular expression like /[a-z, ]/i

<!DOCTYPE html">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Untitled Document</title>
</head>

<body>
<label>full name
<input type="text" name="textfield" onkeydown="return /[a-z, ]/i.test(event.key)"
    onblur="if (this.value == '') {this.value = '';}"
    onfocus="if (this.value == '') {this.value = '';}"/>
</label>
</body>
</html>
like image 51
Gaurav Rana Avatar answered Oct 28 '25 20:10

Gaurav Rana



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!