Here I came up with a robust solution where you can find everything in one place. In function, you can find all types of validation in the same method with the proper message.
For JavaScript Users...
function validateInput(input,type,max,min,confirmPassword ='' ){
input = input.toLocaleString();
const validateMaxLength = (maxLen) => input.length <= maxLen;
const validateMinLength = (minLen) => input.length >= minLen;
const validateNotEmpty = () => input.trim() !== '';
if( !validateMaxLength(max) ){
return {status:'error',msg:'String is too long.'};
}
else if( !validateMinLength(min))
return {status:'error',msg:'String is too short.'};
else if( !validateNotEmpty() ){
return {status:'error',msg:'String can not empty.'};
}
else if( type === 'email'){
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
if( !emailRegex.test(input) ){
return {status:'error',msg:'Email is not valid.'};;
}
return {status:'success',msg:""};
}
else if ( type === 'alphanumeric' )
{
const alphanumericRegex = /^[a-zA-Z0-9\s]+$/;
if( !alphanumericRegex.test(input) ){
return {status:'error',msg:'String is not valid.'};
}
return {status:'success',msg:""};
}
else if ( type === 'onlyString' )
{
const stringRegex = /^[a-zA-Z\s]+$/;
if( !stringRegex.test(input) ){
return {status:'error',msg:'Email is not valid.'};
}
return {status:'success',msg:""};
}
else if ( type === 'onlyNumber' )
{
const numberRegex = /^[0-9]+$/;
if( !numberRegex.test(input) ){
return {status:'error',msg:'Number is not valid.'};
}
return {status:'success',msg:""};
}
else if ( type === 'mobileNumber' )
{
const mobileRegex = /^[+\s0-9]+$/;
if( !mobileRegex.test(input) ){
return {status:'error',msg:'Mobile number is invalid.'};
}
return {status:'success',msg:""};
}
else if ( type === 'alphanumericWithSpecial' )
{
const alphanumericSpecialRegex = /^[a-zA-Z0-9!@#$%&*()_+\-=\[\]{};':"\\|,.<>\/?\s]*$/;
if( !alphanumericSpecialRegex.test(input) ){
return {status:'error',msg:'String is not valid.'};
}
return {status:'success',msg:""};
}
else if ( type === 'webURL' )
{
const urlRegex = /^(ftp|http|https):\/\/[^ "]+$/;
if( !urlRegex.test(input) ){
return {status:'error',msg:'Web url is not valid.'};
}
return {status:'success',msg:""};
}
else if ( type === 'confirmPassword' )
{
if (input !== confirmPassword.toString()) {
return {status:'error',msg:'Passwords do not match.'};
}
return {status:'success',msg:""};
}
return {status:'error',msg:"Invalid Type"};
}
Use Case :
Console.log( validateInput("[email protected]","email",80,10 ) )
For VUE JS :
methods:{
validateInput(input,type,max,min,confirmPassword ='' ){
input = input.toLocaleString();
const validateMaxLength = (maxLen) => input.length <= maxLen;
const validateMinLength = (minLen) => input.length >= minLen;
const validateNotEmpty = () => input.trim() !== '';
if( !validateMaxLength(max) ){
return {status:'error',msg:'String is too long.'};
}
else if( !validateMinLength(min))
return {status:'error',msg:'String is too short.'};
else if( !validateNotEmpty() ){
return {status:'error',msg:'String can not empty.'};
}
else if( type === 'email'){
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
if( !emailRegex.test(input) ){
return {status:'error',msg:'Email is not valid.'};;
}
return {status:'success',msg:""};
}
else if ( type === 'alphanumeric' )
{
const alphanumericRegex = /^[a-zA-Z0-9\s]+$/;
if( !alphanumericRegex.test(input) ){
return {status:'error',msg:'String is not valid.'};
}
return {status:'success',msg:""};
}
else if ( type === 'onlyString' )
{
const stringRegex = /^[a-zA-Z\s]+$/;
if( !stringRegex.test(input) ){
return {status:'error',msg:'Email is not valid.'};
}
return {status:'success',msg:""};
}
else if ( type === 'onlyNumber' )
{
const numberRegex = /^[0-9]+$/;
if( !numberRegex.test(input) ){
return {status:'error',msg:'Number is not valid.'};
}
return {status:'success',msg:""};
}
else if ( type === 'mobileNumber' )
{
const mobileRegex = /^[+\s0-9]+$/;
if( !mobileRegex.test(input) ){
return {status:'error',msg:'Mobile number is invalid.'};
}
return {status:'success',msg:""};
}
else if ( type === 'alphanumericWithSpecial' )
{
const alphanumericSpecialRegex = /^[a-zA-Z0-9!@#$%&*()_+\-=\[\]{};':"\\|,.<>\/?\s]*$/;
if( !alphanumericSpecialRegex.test(input) ){
return {status:'error',msg:'String is not valid.'};
}
return {status:'success',msg:""};
}
else if ( type === 'webURL' )
{
const urlRegex = /^(ftp|http|https):\/\/[^ "]+$/;
if( !urlRegex.test(input) ){
return {status:'error',msg:'Web url is not valid.'};
}
return {status:'success',msg:""};
}
else if ( type === 'confirmPassword' )
{
if (input !== confirmPassword.toString()) {
return {status:'error',msg:'Passwords do not match.'};
}
return {status:'success',msg:""};
}
return {status:'error',msg:"Invalid Type"};
} ,
debounceEmailValidation: _.debounce( function (){
let d = this.validateInput(this.email,'email',50,10 );
if( d.status === 'error'){
this.errors={email:[ d.msg]};
return
}
this.errors={email:[]};
}, 1000),
},
watch:{
'email':{
handler(){
this.debounceEmailValidation();
}
}
},
debounceMobileValidation using debounce is not mandatory. I used it because there was a need for a program.
/^[a-zA-Z]+\/[a-zA-Z]+\/[a-zA-Z]+$/will evaluate it for you