1let username = '';
2username = username.replace(/\s/g,'_');
3username = username.replace(/\-/g,'.');
4username = username.match(/[a-zA-Z0-9\.\s]+/g).join('_');
1# works in most newer browsers
2^(?=.{8,20}$)(?![_.])(?!.*[_.]{2})[a-zA-Z0-9._]+(?<![_.])$
3 └─────┬────┘└───┬──┘└─────┬─────┘└─────┬─────┘ └───┬───┘
4 │ │ │ │ no _ or . at the end
5 │ │ │ │
6 │ │ │ allowed characters
7 │ │ │
8 │ │ no __ or _. or ._ or .. inside
9 │ │
10 │ no _ or . at the beginning
11 │
12 username is 8-20 characters long
13
14# works in all browsers, but does the same as the above RegEx
15^(?=[a-zA-Z0-9._]{8,20}$)(?!.*[_.]{2})[^_.].*[^_.]$
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>