<form class="form-horizontal" name="f2" id="regisform" method="post" onsubmit="return false;" role="form">
<div class="form-group">
<label for="inputpcode3" class="col-sm-2 control-label">Pincode</label>
<div class="col-lg-3">
<input type="text" name="txtpcode1" class="form-control" id="inputpcode3" placeholder="pincode">
</div>
<input type="submit" name="submit" id="regisBtn" class="btn btn-primary" value="Registration">
</form>
The above represents the form. when the submit button pressed the following javascript will fire:
$(document).ready(function(){
$("#regisBtn").click(function(){
var pincode=$("#inputpcode3").val();
if(pincode){
//validation for pincode to check whether the pincode is only numeric or not and the size must be 6 only
}
else
{
alert("wrong");
}
});
});
as the javascript represent i wnt to that validation and display alert box whether that string contains only numbers or not and the size must be fixed of 6 length. and also show if we want to check whether the string contains only alphabets or not? Thanks.
You can use regular expressions to check your requirement:
if(/^\d{6}$/.test(pincode)){
// Passed
}
Regex101 does a good job of explaining exactly what it means:
A Regular Expression, as said above, is probably your best bet...
/^[0-9]*$/
This checks your string and validates only if there are numbers present.
Also to check length is just a matter of if($(this).val().length > 6) { doSomethingElse() || disable Submit Button }
if($(this).val().length > 6) { doSomethingElse() || disable Submit Button }
Check out this awesome online resource: (Really helps clarify) http://www.regextester.com/21
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.