<script type="text/javascript"> var frmvalidator = new Validator("ExporterEditContactPersonForm"); frmvalidator.EnableOnPageErrorDisplay(); frmvalidator.EnableMsgsTogether(); frmvalidator.addValidation("landMark", "regexp=^\S", "Landmark should not start with space"); //\s matched white space and \S matched non white space so you need to check if it not white space frmvalidator.addValidation("landMark", "regexp=^[a-zA-Z0-9]", "Landmark should not start with special character");
var validator = new Validator(["salutations", "fname", "lname"], ["Salutations", "First Name", "Last Name"]); var form = document.getElementById("webform"); validator.validate(form);
Filter.prototype = new Validator(); Filter.prototype.constructor = Filter; Filter.prototype.modify = ... ...
[Test] public void ShouldOnlyValidateAlphaNamesLessThan50Chars() { var validator = new Validator(); Assert.IsTrue(validator.validates("An Alpha Name")); Assert.IsFalse(validator.validates( "A Really Really Long Name that's 51 characters xx")); Assert.IsFalse(validator.validates("A name with 1234 numbers")); }
validator = new Validator() validator.errors.all()
@Test(expected = ValidatorException.class) public void shouldThrowExceptionWhenValueLengthIsGreaterThan10() throws Exception { Validaor validaor = new Validator(); FacesContext context; UIComponent component; Object value;// <-- I don't know what value exactly is. But you have to create one with length less than 10 of its toString() value validaor.validateDC(context,component,value); } @Test public void shouldNotThrowExceptionWhenValueLengthIsLessThan10() throws Exception { Validaor validaor = new Validator(); FacesContext context; UIComponent component; Object value;// create object with length more than 10 chars to of its toString() value validaor.validateDC(context,component,value); }
var frmvalidator = new Validator("myForm"); //where myform is the name/id of your form frmvalidator.EnableOnPageErrorDisplaySingleBox(); frmvalidator.EnableMsgsTogether(); frmvalidator.addValidation("name","req","Please enter your name"); frmvalidator.addValidation("name","maxlen=20", "Max length for name is 20"); frmvalidator.addValidation("name","alpha_s","Name can contain alphabetic chars only"); //add id to form like id="myForm" document.querySelector("#myForm").addEventListener("submit", function(e){ e.preventDefault(); //stop form from submitting //run your validation code var valid = frmvalidator.valid();//your custom validation method if(!valid){ //alert to user } else { //submit form e.submit(); } });
function Validator() { if(this.formIsValid) { alert('Form is valid!'); } else { alert('Form is invalid...'); } } Validator.prototype = { // Notice the .prototype here, it's important! formIsValid: true, enforceTextFieldMinLength: function(field, minLength) { if (!field.value || field.value.length < minLength) { this.formIsValid = false; } }, enforceLabelHasText: function(label) { if (!label.text) { this.formIsValid = false; } } } var a = new Validator();
<form id="ministerial" name="register" action="" method="post"> <label>Title: </label> <input type="text" name="title" value="" /> <label>First Name: </label> <input type="text" name="first_name" value="" /> <label>Last Name: </label> <input type="text" name="last_name" value="" /> <label>Organization: </label> <input type="text" name="organization" value="" /> ... <input type="submit" name="save" value="SAVE REGISTRATION" onclick="return btnSave_click();" /> <input type="submit" name="submit" value="SUBMIT REGISTRATION" onclick="return btnRegister_click();" /> </form> <script type="text/javascript"> var frmvalidator = new Validator("ministerial"); function btnSave_click(){ frmvalidator.clearAllValidations(); frmvalidator.addValidation("first_name","req","Please enter the first name"); frmvalidator.addValidation("last_name","req","Please enter the last name"); return true; } function btnRegister_click(){ frmvalidator.clearAllValidations(); frmvalidator.addValidation("title","req","Please enter a title"); frmvalidator.addValidation("organization","req","Please enter the organization"); return true; } </script>
public class StringValidator { private List<Throwable> exceptions = new ArrayList<>(); private Validator() { } public static StringValidator instance() { return new Validator(); } public StringValidator validate(final String value, final String message) { if (value.isEmpty()) { this.exceptions.add(new IllegalStateException(message)); } return this; } public List<Throwable> get() { return exceptions; } }
mContext = emailEditText.getContext(); mValidator = new Validator(this); mValidator.setValidationListener(this);
var a = new Validator(), b = new Filter(); //They are initialized var tmp = Filter.prototype, key; Filter.prototype = a; for( key in tmp ) { Filter.prototype[key] = tmp[key]; } Filter.prototype.constructor = Filter;
/** * Make the Sample Copyright Alert response. * * @param req * HttpServletRequest object with the client request. * @param res * HttpServletResponse object back to the client. */ protected void respondCopyrightAlertDemo(HttpServletRequest req, HttpServletResponse res) throws ServletException { // the context wraps our real vm attribute set ResourceProperties props = new BaseResourceProperties(); setVmReference("props", props, req); setVmReference("validator", new Validator(), req); setVmReference("sample", Boolean.TRUE.toString(), req); setVmReference("tlang", rb, req); res.setContentType("text/html; charset=UTF-8"); includeVm("vm/access/copyrightAlert.vm", req, res); }
setVmReference("validator", new Validator(), req); setVmReference("props", props, req); setVmReference("tlang", rb, req);