There is such a task text:

<SCRIPT LANGUAGE="javascript" TYPE="text/javascript"> function prepare(form) { var str = form.phone.value var re1 = new RegExp(/[-()/\\]/g) str = str.replace(re1,'') var re2 = new RegExp(/^\d{7}$/) if(!re2.test(str)) { alert('Пожалуйста, проверьте правильность телефонного номера') form.phone.focus() form.phone.select() return false } form.phone.value = str alert('Телефонный номер верен') return true } </SCRIPT> 

It needs to be issued in the finished page (opened by a donkey and checked the number of 7 characters), my exam depends on it, only in my performance it naturally does not work.

  • Firebug launched? - dzhioev
  • No, but the code was checked in the finished page (with all Body / head / title and so on.), which must be obtained, in the donkey by the teacher. - xCoNDoRx
  • one
    What does not work specifically? Launch under Firebug (this is such an extension in Firefox), it will indicate errors to you. - dzhioev

2 answers 2

There was such an option, but did not manage to post it, until everything broke here. The plus is that the event hangs on form.onsubmit, which catches any ways to submit the form - either by clicking on submit, or by a simple enterter in the input field.

 <!DOCTYPE html> <html> <head><title></title> <script type="text/javascript"> function prepare() { var x = document.getElementById('phone'); var v = x.value; v = v.replace(/\D/g, ''); //замена не-цифр if(v.length != 7) { alert('Неправильный номер!'); return false; } alert(v + ' - правильный номер!'); return true; } </script> </head> <body> <form method="post" action="#" onSubmit="return prepare();"> <input type="text" name="phone" id="phone" /><input type="submit" value="test" /> </form> </body> </html> 
     <html> <head> <title> </title> <SCRIPT LANGUAGE="javascript" TYPE="text/javascript"> function prepare(form) { var str = form.phone.value var re1 = new RegExp(/[-()/\\]/g) str = str.replace(re1,'') var re2 = new RegExp(/^\d{7}$/) if(!re2.test(str)) { alert('Пожалуйста, проверьте правильность телефонного номера'); form.phone.focus(); form.phone.select(); return false } form.phone.value = str alert('Телефонный номер верен') return true } </SCRIPT> </head> <body> <form> <input type="text" name="phone" /> <button onclick="prepare(this.form)">ТЫНЦ</button> </form> </body> </html> 

    Here is the result. ) All thanks, closed the topic. )