I only want to get this function working:
function ausPhoneValidate(str){if (/^(?:\\+?(61))? ?(?:\\((?=.*\\)))?(0?[2-57-8])\\)? ?(\\d\\d(?:[- ](?=\\d{3})|(?!\\d\\d[- ]?\\d[- ]))\\d\\d[- ]?\\d[- ]?\\d{3})$/.test(str)){return true;}return false;}
by using following regex:
pattern="^(?:\+?(61))? ?(?:\((?=.*\)))?(0?[2-57-8])\)? ?(\d\d(?:[- ](?=\d{3})|(?!\d\d[- ]?\d[- ]))\d\d[- ]?\d[- ]?\d{3})"
Please check out this fiddle JSfiddle
Additional Information
I have a html regex to validate Australia phone:
<div>
<input class='rform-input' type="text" id="phone" name="phone" required placeholder=" " pattern="^(?:\+?(61))? ?(?:\((?=.*\)))?(0?[2-57-8])\)? ?(\d\d(?:[- ](?=\d{3})|(?!\d\d[- ]?\d[- ]))\d\d[- ]?\d[- ]?\d{3})" />
<label for="last_name">Phone number</label>
<div class="requirements">
Please enter an Australia phone number.
</div>
</div>
now I need a JS function to validate Australia Phone, but, I need this function be written as string, then I will call element.innerHTML = ''
to implement it onto the page. here's what I have got:
function script_render(scr){return `<script type="text/javascript">document.getElementById("booking-p-submit").addEventListener("click", ()=>{${scr}})</script>`;}
function users_script(){
var str = '';
// str += `function emailValidate(str){if (/^\\w+([\\.-]?\\w+)*@\\w+([\\.-]?\\w+)*(\\.\\w{2,3})+$/.test(str)){return true;}return false;}`;
// str += `if(!emailValidate(document.getElementById('email-input').value)){document.getElementById('wform-error').innerHTML = 'Invalid Email'};`;
str += `function ausPhoneValidate(str){if (/^(?:\\+?(61))? ?(?:\\((?=.*\\)))?(0?[2-57-8])\\)? ?(\\d\\d(?:[- ](?=\\d{3})|(?!\\d\\d[- ]?\\d[- ]))\\d\\d[- ]?\\d[- ]?\\d{3})$/.test(str)){return true;}return false;}`
str += `if(!ausPhoneValidate(document.getElementById('phone-input').value)){document.getElementById('wform-error').innerHTML = 'Invalid Phone'};`;
return script_render(str);
}
the lines I hased are validation towards email, it works fine, as I escaped backward slash, but when I execute the second part, I get a SyntaxError: Invalid regular expression: unmatched parentheses"
I do appologize that it may be very hard for you to understand the code and trying to offer some help. also testing is complex. I will be so glad if someone gives some suggestion.