1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
/* check form START */ $("input[name=form_text_2]").mask("+7 (999) 999-9999"); $(document).on('click', '#form_id_1', function() { checkInput('input[name="form_text_1"]', 'string'); checkInput('input[name="form_text_2"]', 'phone'); checkInput('input[name="form_text_3"]', 'string'); }); function checkInput(id, type) { switch (type) { case 'string': checkString(id); break; case 'email': checkEmail(id); break; case 'phone': checkPhone(id); break; } } function checkString(id) { var item = $(id); if (item.val() == '') { item.css('border', '1px solid #da6060'); event.preventDefault(); } else { item.css('border', '1px solid #2b9619'); } } function checkEmail(id) { var item = $(id); var result = isValidEmail(id); if (result === false) { item.css('border', '1px solid #da6060'); event.preventDefault(); } else if (result === true) { item.css('border', '1px solid #2b9619'); } } function isValidEmail(id) { var string = $(id).val(); var atSym = string.lastIndexOf("@"); var space = string.lastIndexOf(" "); if (space > 1) { return false; } if (atSym < 1) { return false; } if (atSym == string.length - 1) { return false; } if (atSym > 64) { return false; } if (string.length - atSym > 255) { return false; } var lastDot = string.lastIndexOf("."); if (lastDot > atSym + 1 && lastDot < string.length - 1) { return true; } if (string.charAt(atSym + 1) == '[' && string.charAt(string.length - 1) == ']') { return true; } return false; } function checkPhone(id) { var item = $(id); phone = item.val(); phone = phone.slice(2); phone = phone.match(/^\([0-9]{3}\)[0-9]{3}-[0-9]{2}\-[0-9]{2}/); if (!phone) { item.css('border', '1px solid #da6060'); event.preventDefault(); } else { item.css('border', '1px solid #2b9619'); } } /* check form END */ |