var name, mobile; var nameRegex = /^([a-zA-Z]+\s)*[a-zA-Z]+$/, mobileRegex = /^\+[1-9]{1}[0-9]{3,14}$/; var country_codes = [ { "id": "+91", "code": "+91", "country": "India", "text": "(+91) India", "pattern": "^\\d{10}$", "patternWithCountryCode": "^([0]|\\+91)?\\d{10}" }, { "id": "+852", "code": "+852", "country": "Hong Kong", "text": "(+852)Hong Kong", "pattern": "^\\d{8}$", "patternWithCountryCode": "^([0]|\\+852)?\\d{8}" }, { "id": "+1", "code": "+1", "country": "USA", "text": "(+1)USA", "pattern": "^\\d{10}$", "patternWithCountryCode": "^([0]|\\+1)?\\d{10}" }, { "id": "+966", "code": "+966", "country": "Saudi Arabia", "text": "(+966)Saudi Arabia", "pattern": "^\\d{9,10}$", "patternWithCountryCode": "^([0]|\\+966)?\\d{9,10}" } ]; function requestDemo() { var nameInput = document.getElementById('name'); var mobileInput = document.getElementById('mobile'); const dto = { name: nameInput.value, mobile: mobileInput.value } console.log(dto); onDemoRequestCall(dto); } function RequestDemo1() { var nameInput = document.getElementById('name1'); var mobileInput = document.getElementById('mobile1'); const dto = { name: nameInput.value, mobile: mobileInput.value } console.log(dto); onDemoRequestCall(dto); } function onDemoRequestCall(dto) { fetch('https://api.tuttory.net/tms-webapi-notificationservice/api/tuttorydemo/request-demo', { method: 'POST', body: JSON.stringify(dto), // string or object headers: { 'Content-Type': 'application/json' } }).then(response => { return response.json() }).then(isSaved => { console.log(isSaved); if (isSaved === true) { alert('Dear Sir/Madam, thanks for taking time to request for a demo. Our team will contact you soon in next 24 hours and arrange for a demo.'); } }); } function onMobileKeyUp(e) { e.preventDefault(); var mobile = document.getElementById('mobile').value; countryCode = country_codes.filter(elem => mobile.match('^' + RegExp.escape(elem.id)))[0]; if (countryCode != undefined && countryCode != null) { document.getElementById('mobile').setAttribute('pattern', countryCode.patternWithCountryCode); } else { document.getElementById('mobile').setAttribute('pattern', mobileRegex); } } function onMobileKeyUp1(e) { e.preventDefault(); var mobile = document.getElementById('mobile1').value; countryCode = country_codes.filter(elem => mobile.match('^' + RegExp.escape(elem.id)))[0]; if (countryCode != undefined && countryCode != null) { document.getElementById('mobile1').setAttribute('pattern', countryCode.patternWithCountryCode); } else { document.getElementById('mobile1').setAttribute('pattern', mobileRegex); } } function onFormSubmit(e, fromdemo1) { e.preventDefault(); if (e.target.checkValidity() === false) { console.log(demoRequestForm); return false; } console.log('executed'); if (fromdemo1) { RequestDemo1(); } else { requestDemo(); } } RegExp.escape = function (s) { return s.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&'); }; // form validation (function () { 'use strict'; window.addEventListener('load', function () { // Fetch all the forms we want to apply custom Bootstrap validation styles to var forms = document.getElementsByClassName('needs-validation'); // Loop over them and prevent submission var validation = Array.prototype.filter.call(forms, function (form) { form.addEventListener('submit', function (event) { if (form.checkValidity() === false) { event.preventDefault(); event.stopPropagation(); } form.classList.add('was-validated'); }, false); }); }, false); })();