我有一个这样的javascript表单:
<form id="loginForm" name="loginForm" action="/create_session/" method="post">{% csrf_token %}
<table border="0" cellpadding="0" cellspacing="0" id="id-form">
<tr>
<th valign="top"></th>
<td>
<div class="error_main">
<p style="color: #009900; font-size: 13px; font-weight: bold;"><span style="color:red;">{{ server_error }}</span> {{ connec_ok }}{{ successful }}</p>
</div>
</td>
<td></td>
</tr>
<tr>
<th valign="top">Session Name :</th>
<td>
<input id="session_name" {{ disabled }} value="{{ session_name }}" name="session_name" class="inp-form" type="text" onchange="validateSessionName()" />
</td>
<td id="session_error"></td>
<td></td>
</tr>
<tr>
<th valign="top">Source Server Path :</th>
<td>
<input id="server_path" {{ disabled }} value="{{ server_path }}" name="server_path" onchange="validatePath()" class="inp-form" type="text" />
</td>
<td id="path_error"><span style="color:black"><em>Eg: 10.1.1.43:/home/sachet/test_data</em></span>
</td>
<td></td>
</tr>
<tr>
<th valign="top">Source username :</th>
<td>
<input id="source_username" {{ disabled }} name="source_username" class="inp-form" type="text" onchange="validateUsername()" />
</td>
<td id="username_error"></td>
<td></td>
</tr>
<tr>
<th valign="top">Source Password :</th>
<td>
<input id="source_password" {{ disabled }} name="source_password" class="inp-form" type="password" onchange="validatePassword()" />
</td>
<td id="password_error"></td>
<td></td>
</tr>
<tr>
<th valign="top"></th>
<td>
<input name="make_default" {{ disabled }} {{ checked }} type="checkbox" id="make_default" />
<label style="font-size: 13px;" for="make_default">Make the session default</span>
</td>
</tr>
<tr>
<th> </th>
<td valign="top">
<input type="submit" name="create_session" value="Submit" id="submit" class="form-submit" />
<input type="reset" value="Reset" class="form-reset" />
<td></td>
</tr>
</table>
<!-- end id-form -->
</form>
我想对给定的表单进行两种验证。除了 Make default 复选框,每个输入框都不能为空。我希望每个字段都满足这个正则表达式:
filter = /^[A-Za-z0-9]+$/;
当用户单击提交按钮而不填写任何值时,我想在每个字段的右侧显示所需的字段。此外,如果用户在填写表单时不满足输入字段,我想显示无效数据,因此我添加了onchange
. 我已经完成了 onchange 部分,如果用户提交空表单,我想在每个字段上显示所需的字段。这是我到目前为止所拥有的:
$(document).ready(function() {
$("#submit").click(function(){
var session_name = $("#session_name").attr("value");
var server_path = $("#server_path").attr("value");
var source_username = $("#source_username").attr("value");
var source_password = $("#source_password").attr("value");
if(session_name==''){
document.getElementById('session_error').innerHTML="Field Required!";
document.getElementById('submit').disabled=false;
}
else if(server_path==''){
document.getElementById('path_error').innerHTML="Field Required!";
document.getElementById('submit').disabled=false;
}
else if(source_username==''){
document.getElementById('username_error').innerHTML="Field Required!";
document.getElementById('submit').disabled=false;
}
else if(source_password==''){
document.getElementById('password_error').innerHTML="Field Required!";
document.getElementById('submit').disabled=false;
}
else{
return true;
}
});
});