├── README.md ├── all-alphabets.js ├── all-num.js ├── alpha-num.js ├── date-validaion.js ├── decimals.js ├── email-val.js ├── ip-validator.js ├── len-check.js ├── non-empty.js ├── valid-pass.js ├── valid-phone.js └── valid-uid.js /README.md: -------------------------------------------------------------------------------- 1 | # JavaScript-Form-Validations 2 | Simple and basic javascript form validations 3 | 4 | ## Table of Validations: 5 | 6 | | S. No. | Type of validation | Link | 7 | |---------|---------------------|-------| 8 | | 1 | Non-empty text field | https://github.com/MainakRepositor/JavaScript-Form-Validations/blob/master/non-empty.js | 9 | | 2 | All alphabetic text | https://github.com/MainakRepositor/JavaScript-Form-Validations/blob/master/all-alphabets.js | 10 | | 3 | All numeric text | https://github.com/MainakRepositor/JavaScript-Form-Validations/blob/master/all-num.js | 11 | | 4 | All floating point numbers | https://github.com/MainakRepositor/JavaScript-Form-Validations/blob/master/decimals.js | 12 | | 5 | Alphanumeric text | https://github.com/MainakRepositor/JavaScript-Form-Validations/blob/master/alpha-num.js | 13 | | 6 | Check for string length | https://github.com/MainakRepositor/JavaScript-Form-Validations/blob/master/len-check.js | 14 | | 7 | Email validation | https://github.com/MainakRepositor/JavaScript-Form-Validations/blob/master/email-val.js | 15 | | 8 | Date validation | https://github.com/MainakRepositor/JavaScript-Form-Validations/blob/master/date-validaion.js | 16 | | 9 | Mobile number validaton |https://github.com/MainakRepositor/JavaScript-Form-Validations/blob/master/valid-phone.js | 17 | | 10 | Password validation | https://github.com/MainakRepositor/JavaScript-Form-Validations/blob/master/valid-pass.js | 18 | | 11 | IP validation | https://github.com/MainakRepositor/JavaScript-Form-Validations/blob/master/ip-validator.js | 19 | | 12 | Valid User ID | https://github.com/MainakRepositor/JavaScript-Form-Validations/blob/master/valid-uid.js | 20 | -------------------------------------------------------------------------------- /all-alphabets.js: -------------------------------------------------------------------------------- 1 | function allLetter(inputtxt) 2 | { 3 | var letters = /^[A-Za-z]+$/; 4 | if(inputtxt.value.match(letters)) 5 | { 6 | return true; 7 | } 8 | else 9 | { 10 | alert("message"); 11 | return false; 12 | } 13 | } 14 | 15 | -------------------------------------------------------------------------------- /all-num.js: -------------------------------------------------------------------------------- 1 | function allnumeric(inputtxt) 2 | { 3 | var numbers = /^[0-9]+$/; 4 | if(inputtxt.value.match(numbers)) 5 | { 6 | alert('Your Registration number has accepted....'); 7 | document.form1.text1.focus(); 8 | return true; 9 | } 10 | else 11 | { 12 | alert('Please input numeric characters only'); 13 | document.form1.text1.focus(); 14 | return false; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /alpha-num.js: -------------------------------------------------------------------------------- 1 | // Function to check letters and numbers 2 | function alphanumeric(inputtxt) 3 | { 4 | var letterNumber = /^[0-9a-zA-Z]+$/; 5 | if((inputtxt.value.match(letterNumber)) 6 | { 7 | return true; 8 | } 9 | else 10 | { 11 | alert("message"); 12 | return false; 13 | } 14 | } 15 | 16 | -------------------------------------------------------------------------------- /date-validaion.js: -------------------------------------------------------------------------------- 1 | function validatedate(inputText) 2 | { 3 | var dateformat = /^(0?[1-9]|[12][0-9]|3[01])[\/\-](0?[1-9]|1[012])[\/\-]\d{4}$/; 4 | // Match the date format through regular expression 5 | if(inputText.value.match(dateformat)) 6 | { 7 | document.form1.text1.focus(); 8 | //Test which seperator is used '/' or '-' 9 | var opera1 = inputText.value.split('/'); 10 | var opera2 = inputText.value.split('-'); 11 | lopera1 = opera1.length; 12 | lopera2 = opera2.length; 13 | // Extract the string into month, date and year 14 | if (lopera1>1) 15 | { 16 | var pdate = inputText.value.split('/'); 17 | } 18 | else if (lopera2>1) 19 | { 20 | var pdate = inputText.value.split('-'); 21 | } 22 | var dd = parseInt(pdate[0]); 23 | var mm = parseInt(pdate[1]); 24 | var yy = parseInt(pdate[2]); 25 | // Create list of days of a month [assume there is no leap year by default] 26 | var ListofDays = [31,28,31,30,31,30,31,31,30,31,30,31]; 27 | if (mm==1 || mm>2) 28 | { 29 | if (dd>ListofDays[mm-1]) 30 | { 31 | alert('Invalid date format!'); 32 | return false; 33 | } 34 | } 35 | if (mm==2) 36 | { 37 | var lyear = false; 38 | if ( (!(yy % 4) && yy % 100) || !(yy % 400)) 39 | { 40 | lyear = true; 41 | } 42 | if ((lyear==false) && (dd>=29)) 43 | { 44 | alert('Invalid date format!'); 45 | return false; 46 | } 47 | if ((lyear==true) && (dd>29)) 48 | { 49 | alert('Invalid date format!'); 50 | return false; 51 | } 52 | } 53 | } 54 | else 55 | { 56 | alert("Invalid date format!"); 57 | document.form1.text1.focus(); 58 | return false; 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /decimals.js: -------------------------------------------------------------------------------- 1 | function CheckDecimal(inputtxt) 2 | var decimal= /^[-+][0-9]+\.[0-9]+[eE][-+]?[0-9]+$/; 3 | if(inputtxt.value.match(decimal)) 4 | { 5 | alert('Correct, try another...') 6 | return true; 7 | } 8 | else 9 | { 10 | alert('Wrong...!') 11 | return false; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /email-val.js: -------------------------------------------------------------------------------- 1 | function ValidateEmail(mail) 2 | { 3 | if (/^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$/.test(myForm.emailAddr.value)) 4 | { 5 | return (true) 6 | } 7 | alert("You have entered an invalid email address!") 8 | return (false) 9 | } 10 | -------------------------------------------------------------------------------- /ip-validator.js: -------------------------------------------------------------------------------- 1 | function ValidateIPaddress(ipaddress) 2 | { 3 | if (/^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/.test(myForm.emailAddr.value)) 4 | { 5 | return (true) 6 | } 7 | alert("You have entered an invalid IP address!") 8 | return (false) 9 | } 10 | -------------------------------------------------------------------------------- /len-check.js: -------------------------------------------------------------------------------- 1 | function lengthRange(inputtxt, minlength, maxlength) 2 | { 3 | var userInput = inputtxt.value; 4 | if(userInput.length >= minlength && userInput.length <= maxlength) 5 | { 6 | return true; 7 | } 8 | else 9 | { 10 | alert("Please input between " +minlength+ " and " +maxlength+ " characters"); 11 | return false; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /non-empty.js: -------------------------------------------------------------------------------- 1 | function required() 2 | { 3 | var empt = document.forms["form1"]["text1"].value; 4 | if (empt == "") 5 | { 6 | alert("Please input a Value"); 7 | return false; 8 | } 9 | else 10 | { 11 | alert('Code has accepted : you can try another'); 12 | return true; 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /valid-pass.js: -------------------------------------------------------------------------------- 1 | function CheckPassword(inputtxt) 2 | { 3 | var passw= /^[A-Za-z]\w{7,14}$/; 4 | if(inputtxt.value.match(passw)) 5 | { 6 | alert('Correct, try another...') 7 | return true; 8 | } 9 | else 10 | { 11 | alert('Wrong...!') 12 | return false; 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /valid-phone.js: -------------------------------------------------------------------------------- 1 | function phonenumber(inputtxt) 2 | { 3 | var phoneno = /^\d{10}$/; 4 | if((inputtxt.value.match(phoneno)) 5 | { 6 | return true; 7 | } 8 | else 9 | { 10 | alert("message"); 11 | return false; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /valid-uid.js: -------------------------------------------------------------------------------- 1 | function userid_validation(uid,mx,my) 2 | { 3 | var uid_len = uid.value.length; 4 | if (uid_len == 0 || uid_len >= my || uid_len < mx) 5 | { 6 | alert("User Id should not be empty / length be between "+mx+" to "+my); 7 | uid.focus(); 8 | return false; 9 | } 10 | return true; 11 | } 12 | --------------------------------------------------------------------------------