I would like to share input validations and javascript utility functins through this post.
1. Date validation using java script
function isValidDate(date){
if( date.match( /^(?:(0[1-9]|1[012])[\- \/.](0[1-9]|[12][0-9]|3[01])[\- \/.](19|20)[0-9]{2})$/ )) {
return true;
}else{
return false;
}
}
This date validation almost covers all the date formats. please let me know if you need validation for other formats . I will prepare it and post it here.
2. SSN validation
var matchArr = ssnIT.match(/^(\d{3})-?\d{2}-?\d{4}$/)
var numDashes = ssnIT.split('-').length - 1;
if (matchArr == null || numDashes == 1) {
alert("Social Security Number must only contain values in NNN-NN-NNNN or NNNNNNNNN formats.")
} else if (parseInt(matchArr[1],10)==0) {
alert("Invalid SSN: SSN's can't start with 000");
}
This is very useful validation. you can validate SSN on the UI using above code snippet. Let me know if
it helps you.
3. only alpha characters validations, no numeric characters
var regex = /^[A-z]+$/
if (!regex.test(Name)) {
alert("input must only contain letters"};
}
Only alpha characters are validated using above function.
4. claculating Age using Javascript
function getAge(dateString) {
var today = new Date();
var birthDate = new Date(dateString);
var age = today.getFullYear() - birthDate.getFullYear();
var m = today.getMonth() - birthDate.getMonth();
if (m < 0 || (m === 0 && today.getDate() < birthDate.getDate())) {
age--;
}
return age;
}
There was requirement to validate the age of the user. I used the above function.
Thats all for now. I will post other useful validations and usefule scripts on this post.. Keep watching..!!
1. Date validation using java script
function isValidDate(date){
if( date.match( /^(?:(0[1-9]|1[012])[\- \/.](0[1-9]|[12][0-9]|3[01])[\- \/.](19|20)[0-9]{2})$/ )) {
return true;
}else{
return false;
}
}
This date validation almost covers all the date formats. please let me know if you need validation for other formats . I will prepare it and post it here.
2. SSN validation
var matchArr = ssnIT.match(/^(\d{3})-?\d{2}-?\d{4}$/)
var numDashes = ssnIT.split('-').length - 1;
if (matchArr == null || numDashes == 1) {
alert("Social Security Number must only contain values in NNN-NN-NNNN or NNNNNNNNN formats.")
} else if (parseInt(matchArr[1],10)==0) {
alert("Invalid SSN: SSN's can't start with 000");
}
This is very useful validation. you can validate SSN on the UI using above code snippet. Let me know if
it helps you.
3. only alpha characters validations, no numeric characters
var regex = /^[A-z]+$/
if (!regex.test(Name)) {
alert("input must only contain letters"};
}
Only alpha characters are validated using above function.
4. claculating Age using Javascript
function getAge(dateString) {
var today = new Date();
var birthDate = new Date(dateString);
var age = today.getFullYear() - birthDate.getFullYear();
var m = today.getMonth() - birthDate.getMonth();
if (m < 0 || (m === 0 && today.getDate() < birthDate.getDate())) {
age--;
}
return age;
}
There was requirement to validate the age of the user. I used the above function.
Thats all for now. I will post other useful validations and usefule scripts on this post.. Keep watching..!!
