


Implementieren der Formularvalidierung basierend auf Bootstrap jQuery.validate_jquery
May 16, 2016 pm 04:26 PMBasierend auf der Bootstrap jQuery.validate Form-Formularvalidierungspraxis-Projektstruktur:
Quellcode-Adresse auf Github: https://github.com/starzou/front-end-example
1. Formular-Formularcode [html]
<!DOCTYPE html>
<html>
<head>
<title>Bootstrap Form Template</title>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" type="text/css" href="plugins/bootstrap/css/bootstrap.css"/>
</head>
<body>
<div class="container">
<h1 class="text-center text-danger">Form 示例</h1>
<form role="form" class="form-horizontal" action="javascript:alert('验证成功,可以提交.');" method="post">
<div class="form-group">
<label class="col-md-2 control-label" for="name">Name</label>
<div class="col-md-10">
<input class="form-control" name="name" type="text" id="name" placeholder="Name" value="" />
</div>
</div>
<div class="form-group">
<label class="col-md-2 control-label" for="exampleInputPassword1">Password</label>
<div class="col-md-10">
<input type="password" name="password" class="form-control" id="exampleInputPassword1" placeholder="Password">
</div>
</div>
<div class="form-group">
<label for="intro" class="control-label col-md-2">Intro</label>
<div class="col-md-10">
<textarea id="intro" class="form-control" rows="3" name="intro" placeholder="Intro"></textarea>
</div>
</div>
<div class="form-group">
<label class="control-label col-md-2">Gender</label>
<div class="col-md-10">
<label class="radio-inline">
<input type="radio" name="gender" value="男" />
boy </label>
<label class="radio-inline">
<input type="radio" name="gender" value="女" />
gril </label>
</div>
</div>
<div class="form-group">
<label for="hobby" class="control-label col-md-2">Hobby</label>
<div class="col-md-10">
<div class="checkbox">
<label>
<input type="checkbox" name="hobby" value="Music">
Music</label>
</div>
<div class="checkbox">
<label>
<input type="checkbox" name="hobby" id="" value="Game" />
Game </label>
</div>
<label class="checkbox-inline">
<input type="checkbox" id="inlineCheckbox1" value="option1">
option1 </label>
<label class="checkbox-inline">
<input type="checkbox" id="inlineCheckbox2" value="option2">
option3</label>
<label class="checkbox-inline">
<input type="checkbox" id="inlineCheckbox3" value="option3">
option3 </label>
</div>
</div>
<div class="form-group">
<label for="sel" class="control-label col-md-2">Select</label>
<div class="col-md-10">
<select multiple="" id="sel" name="sel" class="form-control">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<button type="submit" class="btn btn-primary btn-sm">
Senden
</button>
<button type="reset" class="btn btn-primary btn-sm">
Zurücksetzen
</button>
</div>
</div>
</form>
</div>
<script src="plugins/jquery-1.11.1.js" type="text/javascript" charset="utf-8"></script>
<script src="plugins/bootstrap/js/bootstrap.js" type="text/javascript" charset="utf-8"></script>
<script src="plugins/jquery-validation/dist/jquery.validate.js" type="text/javascript" charset="utf-8"></script>
<script src="scripts/form.js" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript" charset="utf-8">
MyValidator.init();
</script>
</body>
</html>
Sie können jquery.js,bootstrap.js,jquery.validate.js verwenden
2、form.js 代码[javascript]
var MyValidator = function() {
var handleSubmit = function() {
$('.form-horizontal').validate({
errorElement: 'span',
errorClass: 'help-block',
focusInvalid: falsch,
Regeln: {
Name: {
Erforderlich: wahr
},
Passwort: {
Erforderlich: wahr
},
Einleitung: {
Erforderlich: wahr
}
},
Nachrichten: {
Name: {
erforderlich: „Benutzername ist erforderlich.“
},
Passwort: {
erforderlich: „Passwort ist erforderlich.“
},
Einleitung: {
erforderlich: „Einführung ist erforderlich.“
}
},
hervorheben: function(element) {
$(element).closest('.form-group').addClass('has-error');
},
Erfolg: function(label) {
label.closest('.form-group').removeClass('has-error');
label.remove();
},
errorPlacement: function(error, element) {
element.parent('div').append(error);
},
commitHandler: function(form) {
form.submit();
}
});
$('.form-horizontal input').keypress(function(e) {
if (e.which == 13) {
If ($('.form-horizontal').validate().form()) {
$('.form-horizontal').submit();
return false; });
}
Geben Sie {
zurück init: function() {
handleSubmit(); };
}();
Wirkung:
Ein ziemlich guter Spezialeffekt für die Formularvalidierung. Ich kann ihn jedem empfehlen, der ihn in seinen eigenen Projekten verwenden kann.

Heißer Artikel

Hot-Tools-Tags

Heißer Artikel

Heiße Artikel -Tags

Notepad++7.3.1
Einfach zu bedienender und kostenloser Code-Editor

SublimeText3 chinesische Version
Chinesische Version, sehr einfach zu bedienen

Senden Sie Studio 13.0.1
Leistungsstarke integrierte PHP-Entwicklungsumgebung

Dreamweaver CS6
Visuelle Webentwicklungstools

SublimeText3 Mac-Version
Codebearbeitungssoftware auf Gottesniveau (SublimeText3)

Heiße Themen

So führen Sie Bootstrap in Eclipse ein

So führen Sie eine Idee in Bootstrap ein

Nach 750.000 Runden Einzelkampf zwischen großen Modellen gewann GPT-4 die Meisterschaft und Llama 3 belegte den fünften Platz

So lesen Sie die Ergebnisse des Bootstrap-Mediation-Effekt-Tests in Stata

Was ist der Unterschied zwischen Bootstrap und Springboot?

So lesen Sie die Ergebnisse des Bootstrap-Mediation-Tests

So verwenden Sie Bootstrap, um den Mediationseffekt zu testen

So exportieren Sie die Ergebnisse des Befehls Bootstrap Test Mediation Effect Stata
