1. Phone number validation using jquery,
<html>
<head>
//Import jquery.Min.js
<script type="text/javascript">
$.fn.isPhoneNumber = function(options) {
intRegex = /^[0-9]{1,10}$/;
phoneNumber = this.val();
if ((phoneNumber.length != 10) || (!intRegex.test(phoneNumber))) {
this.val("");
this.attr("placeholder", "Invalid Phone Number");
return false;
}
return true;
};
function validate() {
$("#checkPhoneNumber").isPhoneNumber(this);
return false;
}
</script>
</head>
<body>
<input type="text" id="checkPhoneNumber" value="ASDFG77" />
<input type="button" onclick="return validate()" value="Validate" />
</body>
</html>2. Postal code validation,
<html>
<head>
//Import jquery.Min.js
<script type="text/javascript">
$.fn.isPostalCodeNumber = function(options) {
intRegex = /^[0-9]{1,4}$/;
postalcodeNumber = this.val();
if ((postalcodeNumber.length != 10) || (!intRegex.test(postalcodeNumber))) {
this.val("");
this.attr("placeholder", "Invalid Postal Code Number");
return false;
}
return true;
};
function validate() {
$("#checkPostalCodeNumber").isPostalCodeNumber(this);
return false;
}
</script>
</head>
<body>
<input type="text" id="checkPostalCodeNumber" value="ASDFG77" />
<input type="button" onclick="return validate()" value="Validate" />
</body>
</html>3. English validation,
<html>
<head>
//Import jquery.Min.js
<script type="text/javascript">
var englishAlphabetAndWhiteSpace = /^[a-zA-Z ]*$/;
if (!englishAlphabetAndWhiteSpace.test(this.val())) {
this.val("");
this.attr("placeholder", "Invalid English Alphabets");
return false;
}
return true;
};
function validate() {
$("#checkEnglishAlphabets").isEnglishAlphabets(this);
return false;
}
</script>
</head>
<body>
<input type="text" id="checkEnglishAlphabets" value="ASDFG77" />
<input type="button" onclick="return validate()" value="Validate" />
</body>
</html>4. Get the selected value form drop down,
<html>
<head>
//Import jquery.Min.js
</head>
<body>
<select id="countries">
<option value="1">India</option>
<option value="2">China</option>
<option value="3">Russia</option>
<option value="4">Srilanka</option>
<option value="5">Pakistan</option>
</select>
<input type="button" onclick="return getSelected()" value="Get Selected Value" />
<script type="text/javascript">
function getSelected() {
alert($("#countries option:selected").text());
alert($("#countries").val());
return false;
}
</script>
</body>
</html>