用正则表达式验证用户名输入的正确性
EditBox:
| 有效等价类 | 无效等价类 |
| length:1--6 | length<1orlength>6 |
| char:a-z A-Z 0-9 | other chars |
图片如下:

代码采用javascript实现,代码:
<html> <head> <script type="text/javascript"> function test(){ var name=document.getElementById('name').value; if(name=="") { window.alert("您的用户名为空"); } else { var n=name.length; reg=/^[a-zA-Z0-9_]+$/; if(n<1||n>6) { window.alert("用户名非法") } else if(!reg.test(name)) { window.alert("用户名非法"); } } } </script> </head> <body> <input type="text" id="name" /><br/> <input type="button" onclick="test()" value="确定" /> </body> </html>
测试用例:
| 测试用例 | 预期输出 |
| j | 有效输入 |
| k | 有效输入 |
| M | 有效输入 |
| Y | 有效输入 |
| 8 | 有效输入 |
| 1 | 有效输入 |
| ABCDEF | 有效输入 |
| abcdef | 有效输入 |
| 000000 | 有效输入 |
| aA0aA0 | 有效输入 |
| ; | 无效输入 |
| ,,,,,, | 无效输入 |
| ahskdhj | 无效输入 |
| AJIKHIG | 无效输入 |
| 1230987 | 无效输入 |

浙公网安备 33010602011771号