javascript中的验证
Validations in javascript
本文关键字:验证 javascript 更新时间:2023-09-26
我在django中有一个自定义表单。它工作正常,每当我对它应用javascript验证时,验证都不起作用。我想用javascript验证它。它显示了警报消息,但没有在表单上重定向。
形式:-
<form method="POST" action="#" class="form-horizontal" id="userform" name="uform" enctype="multipart/form-data" >{% csrf_token %}
<fieldset>
<div class="control-group formSep">
<label for="u_id" class="control-label">App Id </label>
<div class="controls">
<input type="text" id="appid" class="input-xlarge" name="appid" value="" />
</div>
</div>
<div class="control-group">
<div class="controls">
<button class="btn btn-gebo" type="submit" name="asubmit">Submit</button>
<input type="reset" name="reset" value="Cancel" class="btn btn-gebo" />
</div>
</div>
</fieldset>
</form>
在视图.py:-
@csrf_exempt
def applicationform(request):
if request.method == 'POST':
getappid = request.POST['appid']
getjobtitle=request.POST['jobtitle']
odeskid=request.POST['odeskid']
clientspent=request.POST['client_spent']
jobtype=request.POST['jobtype']
notestype=request.POST['notes']
request.session['setid'] = request.POST['appid']
if getappid == '':
return HttpResponse('<script> alert("fill app id"); </script>')
else:
getintable = application(app_id = request.POST['appid'], job_title = request.POST['jobtitle'], odesk_id = request.POST['odeskid'],client_spent = request.POST['client_spent'], job_type = request.POST['jobtype'],notes_type = request.POST['notes'])
getintable.save()
return HttpResponseRedirect('/applicationview/')
else:
return render_to_response('applicationform.html')
在警报语句中执行脚本后重定向
@csrf_exempt
def applicationform(request):
if request.method == 'POST':
getappid = request.POST['appid']
getjobtitle=request.POST['jobtitle']
odeskid=request.POST['odeskid']
clientspent=request.POST['client_spent']
jobtype=request.POST['jobtype']
notestype=request.POST['notes']
request.session['setid'] = request.POST['appid']
if getappid == '':
return HttpResponse('<script> alert("fill app id"); document.location.href="redirect url" </script>') #change here
else:
getintable = application(app_id = request.POST['appid'], job_title = request.POST['jobtitle'], odesk_id = request.POST['odeskid'],client_spent = request.POST['client_spent'], job_type = request.POST['jobtype'],notes_type = request.POST['notes'])
getintable.save()
return HttpResponseRedirect('/applicationview/')
else:
return render_to_response('applicationform.html')
表单操作需要URL
<form method="POST" action="/applicationview/" class="form-horizontal" id="userform" name="uform" enctype="multipart/form-data" >
使用return False
$('#userform').submit(function(){
var textVal = $("#appid").val();
if(textVal == "") {
alert('Text Field Cannot be Empty');
return false;
})
如果您希望保持在同一页面中,请使用return false
或
如果要重定向到特定页面,请使用update_content_div('url')
相关文章:
- 验证Javascript中的Textarea
- 表单验证JavaScript编号总是返回false
- 验证javascript中的输入没有'不起作用
- 验证JavaScript中是否存在变量
- 验证javascript中的输入是否在2个数字之间
- 禁止的正则表达式._或.-(电子邮件验证/javascript)
- 表单验证Javascript没有响应
- 用于验证 JavaScript 中的字母数字值的正则表达式
- 如何使用正则表达式验证 JavaScript 中的自定义 URL
- 按 ID 验证 JavaScript 中的列表框和单选按钮
- 如何验证 JavaScript 和 html 代码
- 表单验证 - Javascript 删除空格电话号码
- 用于验证 JavaScript 中的条件的正则表达式
- 仅当浏览器无法验证表单时才使用表单验证 JavaScript
- 多个复选框验证 JavaScript
- 验证 Javascript 中的多个部分
- 使用 Jquery 验证 JavaScript 对象是否为空
- 警报框中的基本验证 Javascript
- 登录验证 JavaScript 无法正常工作
- 验证 JavaScript 正则表达式