Pattern Matching In JavaScript

Put Your Plan Into Action

Pattern Matching In JavaScript

Simple pattern matching program In javascript

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE html>
<html>
	<head>
		<title> USN VALIDATION </title>
	</head>
	<body>
		<script type="text/javascript">
			function func(usn){
				var pattern=/^[1-4][A-Z]{2}[0-9]{2}[A-z]{2}[0-9]{3}$/
				if(!usn.value.match(pattern) || usn.value.length==0){
					alert("INVALID USN\n ENTER A VALID USN");
					location.reload();
				}
				else{
					alert("USN VALID");
					location.reload();
				}
			}		
		</script>
		<form action="">
			<p> 
				USN: <input type="text" name="usn"/>
				<input type="button" value="validate" onclick="func(usn)"/>
			</p>
		</form>
	</body>
</html>

 

Leave a Reply

Your email address will not be published. Required fields are marked *