1function validateUserName(username){
2 var usernameRegex = /^[a-zA-Z0-9]+$/;
3 return usernameRegex.test(username);
4}
1<!DOCTYPE html>
2<html>
3<head>
4 <title></title>
5</head>
6<body>
7<form method="post" action="">
8 username:<input type="text" id="name" onkeyup="validation()">
9 </form>
10</body>
11<script type="text/javascript">
12 function validation(){
13 var username=document.getElementById("name").value;///get id with value
14 var usernamepattern=/^[A-Za-z .]{3,15}$/;////Regular expression
15 if(usernamepattern.test(username))
16 {
17 document.getElementById("name").style.backgroundColor='yellow';
18 }
19 else
20 {
21 document.getElementById("name").style.backgroundColor='red'; }
22 }
23
24</script>
25</html>