RT# 77504 - Documented javascript.
[freeside.git] / httemplate / elements / validate_password.html
1 <%doc>
2
3 To validate passwords via javascript/xmlhttp:
4
5   <INPUT ID="password_field" TYPE="text">
6   <DIV ID="password_field_result">
7   <& '/elements/validate_password.html', 
8       fieldid  => 'password_field',
9       svcnum   => $svcnum,
10       pkgnum   => $pkgnum, # used if the service doesn't exist yet
11   &>
12
13 The ID of the input field can be anything;  the ID of the DIV in which to display results
14 should be the input id plus '_result'.
15
16 </%doc>
17
18 <& '/elements/xmlhttp.html',
19     'url'  => $p.'misc/xmlhttp-validate_password.html',
20     'subs' => [ 'validate_password' ],
21     'method' => 'POST', # important not to put passwords in url
22 &>
23 <SCRIPT>
24 function add_password_validation (fieldid, submitid) {
25   var inputfield = document.getElementById(fieldid);
26   inputfield.onkeydown = function(e) {
27     var key;
28     if (window.event) { key = window.event.keyCode; }
29     else { key = e.which; } // for ff browsers
30     // some browsers allow the enter key to submit a form even if the submit button is disabled
31     // below prevents enter key from submiting form if password has not been validated.
32     if (key == '13') {
33       var check = checkPasswordValidation();
34       return check;
35     }
36   }
37   inputfield.onkeyup = function () {
38     var fieldid = this.id+'_result';
39     var resultfield = document.getElementById(fieldid);
40     if (this.value) {
41       resultfield.innerHTML = '<SPAN STYLE="color: blue;">Validating password...</SPAN>';
42       validate_password('fieldid',fieldid,'svcnum','<% $opt{'svcnum'} %>','pkgnum','<% $opt{'pkgnum'} %>','contactnum','<% $opt{'contactnum'} %>','password',this.value,
43         function (result) {
44           result = JSON.parse(result);
45           var resultfield = document.getElementById(result.fieldid);
46           if (resultfield) {
47             var errorimg = '<IMG SRC="<% $p %>images/error.png" style="width: 1em; display: inline-block; padding-right: .5em">';
48             var validimg = '<IMG SRC="<% $p %>images/tick.png" style="width: 1em; display: inline-block; padding-right: .5em">';
49             if (result.valid) {
50               resultfield.innerHTML = validimg+'<SPAN STYLE="color: green;">Password valid!</SPAN>';
51               if (submitid){ document.getElementById(submitid).disabled = false; }
52             } else if (result.error) {
53               resultfield.innerHTML = errorimg+'<SPAN STYLE="color: red;">'+result.error+'</SPAN>';
54               if (submitid){ document.getElementById(submitid).disabled = true; }
55             } else {
56               result.syserror = result.syserror || 'Server error';
57               resultfield.innerHTML = errorimg+'<SPAN STYLE="color: red;">'+result.syserror+'</SPAN>';
58               if (submitid){ document.getElementById(submitid).disabled = true; }
59             }
60           }
61         }
62       );
63     } else {
64       resultfield.innerHTML = '';
65     }
66   };
67 }
68
69 add_password_validation('<% $opt{'fieldid'} %>', '<% $opt{'submitid'} %>');
70 </SCRIPT>
71
72 <%init>
73 my %opt = @_;
74 </%init>
75
76