RT# 75817 - Some small updates to be able to backport to V3 because of no cust_contac...
[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.onkeyup = function () {
27     var fieldid = this.id+'_result';
28     var resultfield = document.getElementById(fieldid);
29     if (this.value) {
30       resultfield.innerHTML = '<SPAN STYLE="color: blue;">Validating password...</SPAN>';
31       validate_password('fieldid',fieldid,'svcnum','<% $opt{'svcnum'} %>','pkgnum','<% $opt{'pkgnum'} %>','contactnum','<% $opt{'contactnum'} %>','password',this.value,
32         function (result) {
33           result = JSON.parse(result);
34           var resultfield = document.getElementById(result.fieldid);
35           if (resultfield) {
36             var errorimg = '<IMG SRC="<% $p %>images/error.png" style="width: 1em; display: inline-block; padding-right: .5em">';
37             var validimg = '<IMG SRC="<% $p %>images/tick.png" style="width: 1em; display: inline-block; padding-right: .5em">';
38             if (result.valid) {
39               resultfield.innerHTML = validimg+'<SPAN STYLE="color: green;">Password valid!</SPAN>';
40               if (submitid){ document.getElementById(submitid).disabled = false; }
41             } else if (result.error) {
42               resultfield.innerHTML = errorimg+'<SPAN STYLE="color: red;">'+result.error+'</SPAN>';
43               if (submitid){ document.getElementById(submitid).disabled = true; }
44             } else {
45               result.syserror = result.syserror || 'Server error';
46               resultfield.innerHTML = errorimg+'<SPAN STYLE="color: red;">'+result.syserror+'</SPAN>';
47               if (submitid){ document.getElementById(submitid).disabled = true; }
48             }
49           }
50         }
51       );
52     } else {
53       resultfield.innerHTML = '';
54     }
55   };
56 }
57
58 add_password_validation('<% $opt{'fieldid'} %>', '<% $opt{'submitid'} %>');
59 </SCRIPT>
60
61 <%init>
62 my %opt = @_;
63 </%init>
64
65