Add email format validation to the input text field in the setup modal.
[arvados.git] / apps / workbench / app / views / users / _setup_popup.html.erb
1   <div class="modal-body">
2     <% if @object%>
3       <% uuid = @object.uuid %>
4       <% email = @object.email %>
5     <% end %>
6     <% disable_email = uuid != nil %>
7     <% identity_url_prefix = @current_selections[:identity_url_prefix] %>
8     <% disable_url_prefix = identity_url_prefix != nil %>
9     <% selected_repo = @current_selections[:repo_name] %>
10     <% selected_vm = @current_selections[:vm_uuid] %>
11     
12     <%= form_tag setup_user_path, {id: 'setup_form', name: 'setup_form', method: 'get', 
13                     class: 'form-search', remote: true} do %>
14       <input id="user_uuid" maxlength="250" name="user_uuid" type="hidden" value="<%=uuid%>">
15       <div>
16         <table><tbody>
17           <tr>
18             <% if disable_email %>
19               <td> Email </td>
20               <td> <input id="email" maxlength="250" name="email" type="text" value="<%=email%>" 
21                     disabled=true > </td>
22             <% else %>
23               <td> Email </td>
24               <td> <input id="email" maxlength="250" name="email" type="text" > </td>
25             <% end %>
26           </tr>
27           <tr>
28             <% if disable_url_prefix %>
29               <td> Identity URL Prefix &nbsp </td>
30               <td> <input id="openid_prefix" maxlength="250" name="openid_prefix" type="text" 
31                     value="<%=identity_url_prefix%>" disabled=true > </td>
32             <% else %>
33               <td> Openid Prefix </td>
34               <td> <input id="openid_prefix" maxlength="250" name="openid_prefix" type="text" 
35                     value="<%= Rails.configuration.default_openid_prefix %>"> </td>
36             <% end %>
37           </tr>
38           <tr>
39             <td> Repository Name </td>
40             <td> <input id="repo_name" maxlength="250" name="repo_name" type="text" value="<%=selected_repo%>">
41           </tr>
42           <tr>
43             <td> Virtual Machine </td>
44             <td>
45               <select name="vm_uuid">
46                 <% if selected_vm %>
47                   <option value=""> Choose One: </option>
48                 <% else %>
49                   <option value="" selected> Choose One: </option>
50                 <% end %>
51                 <% @vms.each do |vm| %>
52                   <% if selected_vm == vm.uuid %>
53                     <option value=<%=vm.uuid%> selected> <%=vm.hostname%> </option>
54                   <% else %> 
55                     <option value=<%=vm.uuid%> > <%=vm.hostname%> </option>
56                   <% end %>
57                 <% end %>
58               </select>
59             </td>
60           </tr>
61         </tbody></table>
62       </div>
63       <div align="center">
64         <br/>
65         <button type="submit" id="register" class="btn btn-primary" autofocus>Submit</button>
66         <button class="btn btn-primary" data-dismiss="modal" aria-hidden="true">Cancel</button>
67       </div>
68     <% end %>
69   </div>
70
71 <script>
72   var $input = $('input:text'),
73   $register = $('#register');
74
75   var email_disabled = document.forms["setup_form"]["email"].disabled;
76   var email_value = document.forms["setup_form"]["email"].value;
77   var prefix_value = document.forms["setup_form"]["openid_prefix"].value;
78   if ((email_disabled == false) && (email_value == null || email_value == "" || 
79         prefix_value == null || prefix_value == "")) {
80     $register.attr('disabled', true);
81   }
82
83   $input.keyup(function() {
84     var trigger = false;
85
86     var email_disabled = document.forms["setup_form"]["email"].disabled;
87     var email_value = document.forms["setup_form"]["email"].value;
88     var prefix_value = document.forms["setup_form"]["openid_prefix"].value;
89
90     var emailRegExp = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
91     var validEmail = false;
92     if (emailRegExp.test(email_value )) {
93       validEmail = true;
94     }
95
96     if ((email_disabled == false) && (!validEmail || email_value == null || 
97             email_value == "" || prefix_value == null || prefix_value == "")){
98       trigger = true;
99     }
100
101     trigger ? $register.attr('disabled', true) : $register.removeAttr('disabled');
102   });
103 </script>