JSFiddle - React, Tailwind, and code Playground

by John Grivas

HTML

<script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.13.0/jquery.validate.js"></script>
<div id="main">
	<form class="cmxform" id="texttests" method="get" action="foo.html">
		<h2 id="summary"></h2>
		<fieldset>
			<legend>Example with custom methods and heavily customized error display</legend>
			<table>
				<tr>
					<td>
						<label for="name">Your Name</label>
					</td>
					<td>
                        <input id="number" name="name" title="Please enter a number with at least 3 and max 15 characters!"/>
					</td>
					<td></td>
				</tr>
				<tr>
					<td>
						<label for="email">Email</label>
					</td>
					<td>
                        <input name="email" id="email"/>
					</td>
					<td></td>
				</tr>
				<tr>
					<td>
						<label for="message">Message</label>
					</td>
					<td>
                        <input id="message" name="message" title="Please enter the correct result!"/>
					</td>
					<td></td>
				</tr>
			</table>
            <input class="submit" type="submit" value="Submit"/>
		</fieldset>
	</form>
	<h3 id="warning">Your form contains tons of errors! Please try again.</h3>

</div>

CSS

form.cmxform {
		width: 50em;
	}
	em.error {
		background:url("images/unchecked.gif") no-repeat 0px 0px;
		padding-left: 16px;
	}
	em.success {
		background:url("images/checked.gif") no-repeat 0px 0px;
		padding-left: 16px;
	}
	form.cmxform label.error {
		margin-left: auto;
		width: 250px;
	}
	em.error {
		color: black;
	}
	#warning {
		display: none;
	}

JavaScript

// extend the current rules with new groovy ones

	// this one requires the text "buga", we define a default message, too
	$.validator.addMethod("buga", function(value) {
		return value == "buga";
	}, 'Please enter "buga"!');

	// this one requires the value to be the same as the first parameter
	$.validator.methods.equal = function(value, element, param) {
		return value == param;
	};

	$().ready(function() {
		var validator = $("#texttests").bind("invalid-form.validate", function() {
			$("#summary").html("Your form contains " + validator.numberOfInvalids() + " errors, see details below.");
		}).validate({
			debug: true,
			errorElement: "em",
			errorContainer: $("#warning, #summary"),
			errorPlacement: function(error, element) {
				error.appendTo(element.parent("td").next("td"));
			},
			success: function(label) {
				label.text("ok!").addClass("success");
			},
			rules: {
				number: {
					required: true,
					minlength: 3,
					maxlength: 15,
					number: true
				},
				secret: "buga",
				math: {
					equal: 11
				}
			}
		});

	});