
How to write javascript regex validation for alphanumeric?

<html>
<head>
<title> alphanumeric validation using regex</title>
<script type="text/javascript">
function validate() {
var name = document.getElementById("name").value;
var pattern = /^[A-Za-z0-9 ]{3,20}$/;
if (pattern.test(name)) {
alert(name +" has alphanumeric value");
return true;
} else {
alert("Name is not valid.Please input alphanumeric value!");
return false;
}
}
</script>
</head>
<body>
<h2>Validating Alphanumeric value of textBox..</h2>
Email Id :
<input type="text" name="name" id="name" />
<input type="submit" value="Check" onclick="validate();"/>
</body>
</html>

Alphanumeric contains alphabets(A-Z,a-z) and numbers (0-9).So for validation of alphanumeric you have to check the pattern of combination of these two. Sometimes space is also included in it. In pattern /^[A-Za-z0-9 ]$/ /../(forward slashes) is used to quote your regular expression. ^ shows beginning of string. [..] any letter enclosed with in. $ is used for ending the string. test() method takes one argument and check that to the pattern.