我有一个带有输入字段的Bootstrap 4表单,名为runname.我想在输入字段上执行以下验证: runname不能为空 runname不能包含空格 runname还没有以前使用过 我已经有一个表单的代码,如果输入字段为
> runname不能为空
> runname不能包含空格
> runname还没有以前使用过
我已经有一个表单的代码,如果输入字段为空,则使用custom Bootstrap styles给出错误:
// JavaScript for disabling form submissions if there are invalid fields (function() { 'use strict'; window.addEventListener('load', function() { // Fetch all the forms we want to apply custom Bootstrap validation styles to var forms = document.getElementsByClassName('needs-validation'); // Loop over them and prevent submission var validation = Array.prototype.filter.call(forms, function(form) { form.addEventListener('submit', function(event) { if (form.checkValidity() === false) { event.preventDefault(); event.stopPropagation(); } form.classList.add('was-validated'); }, false); }); }, false); })();
<html lang="en"> <head> <!-- Required meta tags --> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> <!-- Bootstrap CSS --> <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css" integrity="sha384-WskhaSGFgHYWDcbwN70/dfYBj47jz9qbsMId/iRN3ewGhXQFZCSftd1LZCfmhktB" crossorigin="anonymous"> </head> <body class="bg-light"> <div class="container"> <div class="col-md-12 order-md1"> <form class="needs-validation" novalidate method="post" action="#"> <div class="form-group row"> <label for="inputRunname" class="col-sm-2 col-form-label">Run name</label> <div class="col-sm-10"> <input type="text" class="form-control" id="inputRunname" name="runname" placeholder="Run name" required> <div class="invalid-feedback"> Please enter a run name </div> </div> </div> <div class="form-group row"> <div class="col-sm-10"> <button type="submit" class="btn btn-primary">Submit</button> </div> </div> </form> </div> </div> <!-- Optional JavaScript --> <!-- jQuery first, then Popper.js, then Bootstrap JS --> <script src="http://img.558idc.com/uploadfile/allimg/210612/0954325195-0.jpg" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script> <script src="http://img.558idc.com/uploadfile/allimg/210612/0954325632-2.jpg" integrity="sha384-smHYKdLADwkXOn1EmN1qk/HfnUcbVRZyYmZ4qpPea6sjB/pTJ0euyQp0Mk8ck+5T" crossorigin="anonymous"></script> </body> </html>
我有一些Javascript来检查输入是否包含空格:
function cannotContainWhiteSpace(input, errorId, name) { var value = input.value; var errMsgHolder = document.getElementById(errorId); if (!(/^\S*$/.test(value))) { errMsgHolder.innerHTML = 'The ' + name + ' cannot contain whitespace'; input.focus(); return false; } }
我在Cherrypy后端也有一些Python代码,它在数据库中查找运行名是否已经存在:
try: myConnection = mysql.connector.connect(host=self.database['host'], user=self.database['user'], passwd=self.database['passwd'], db=self.database['db']) cursor = myConnection.cursor(buffered=True) # unless overriden by the force flag, check whether the runname has already been used before if not force: reusedrunquery = "SELECT run FROM logs WHERE run = %s AND errormessage IS NULL" cursor.execute(reusedrunquery, (runname,)) if cursor.fetchall(): flag = True cherrypy.session['reusedRun'] = True myConnection.close() except mysql.connector.Error as err: return self.database_failure(str(err))
但是我不知道如何将所有这些不同的部分组合在一起以获得一个表单,其中我同时具有两个客户端验证和服务器端验证.
在提交事件时,您的后端应该有一个实际拦截请求的方法,我认为您应该能够与后端的逻辑建立连接.这是他们的步骤:
>表格编译正确
> Http POST请求启动onSubmit事件
>后端接收请求并通过收集负责方法上的数据来应用进一步的逻辑以接收Http POST请求
否则,您可能尝试进行AJAX调用,在该调用上将执行客户端验证,然后它将调用服务器端方法/类以检查已经使用了runname.