1<html>
2<head>
3 <title></title>
4</head>
5<body>
6<form method="post" action="">
7 username:<input type="text" id="name" onkeyup="validation()">
8 </form>
9</body>
10<script type="text/javascript">
11 function validation(){
12 var username=document.getElementById("name").value;///get id with value
13 var usernamepattern=/^[A-Za-z .]{3,15}$/;////Regular expression
14 if(usernamepattern.test(username))
15 {
16 document.getElementById("name").style.backgroundColor='yellow';
17 }
18 else
19 {
20 document.getElementById("name").style.backgroundColor='red'; }
21 }
22
23</script>
24</html>
1
2
3
4
5 const checkUsername = () => {
6
7 let valid = false;
8 const min = 3,
9 max = 25;
10 const username = usernameEl.value.trim();
11
12 if (!isRequired(username)) {
13 showError(usernameEl, 'Username cannot be blank.');
14 } else if (!isBetween(username.length, min, max)) {
15 showError(usernameEl, `Username must be between ${min} and ${max} characters.`)
16 } else {
17 showSuccess(usernameEl);
18 valid = true;
19 }
20 return valid;
21}Code language: JavaScript (javascript)