-1

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.

  • Welcome to SO! May I ask: why are you stringifying and injecting new scripts instead of simply running the code in the current script? Also, does the regex work or not? If not, what constitutes an Australian phone number, exactly? I don't see any mismatched parenthesis here so I can't reproduce the error. Thanks for clarifying. – ggorlen Aug 15 '20 at 02:47
  • @ggorlen: that's very nice thanks. I am building an admin page, that illustrates tables of database within one-to-many relationship. because there are quite a few tables, I wrote an jsParser in php that allow administer to resort table sequence by clicking on the table header, also, if they click on the row, it brings them to a child table that associated with it, it's looped until there are no more child tables. due to this, 90% of my code in the admin page is javascript, by using `element.innerHTML`, I inject html into one div `
    `
    –  Aug 15 '20 at 02:54
  • what I am trying to do now, is to archive `delete` and `edit`, `delete` is pretty easy, I only need to write a confirm form. but `edit`, man, I need to revalidate every stuff that the administer has entered before pass them to the database, and there is no tags in html page that I can access from button line javascript, since most of my contents are temporarily generated from parser. therefore, I need to write scripts in string format and add them to the parser immediately after the code is injected, so that I can access to the tag. –  Aug 15 '20 at 03:01

2 Answers2

0

you have written extra (maybe) ')' after )?(0?[2-57-8])\ try this

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);
}
Kuldip Chaudhari
  • 1,112
  • 4
  • 8
  • Okay. Why is **false;)}** in function **ausPhineVallidate** at the end I think there is no corresponding opening bracket for it. – Kuldip Chaudhari Aug 15 '20 at 03:42
  • sorry that was a typo, I have fixed that in the question, after fixing that, there is still `SyntaxError: Invalid regular expression: unmatched parentheses"` –  Aug 15 '20 at 03:48
0

I have checked with the python script also, there ab extra ')' after *)?(0?[2-57-8])* excute this in py

t="^(?:\+?(61))? ?(?:\((?=.*\)))?(0?[2-57-8])\)? ?(\d\d(?:[- ](?=\d{3})|(?!\d\d[- ]?\d[- ]))\d\d[- ]?\d[- ]?\d{3})"
print(list(t).count('('), 
list(t).count(')'))

c1, c2=0, 0
for i,c in enumerate(list(t)):
    if c=='(':
        c1+=1
    elif c==')':
        c2+=1
    
    if c2>c1:
        print(t[i:i+10])
        print(i)
        break
Kuldip Chaudhari
  • 1,112
  • 4
  • 8