Forums

Conditionally enable validation (if HTML 5 not supported)

petersen 04 Aug, 2011
Is there a way to conditionally enable the Chronoform (v4) validation if HTML 5 input types aren't supported? I've got Modernizr in my template so can check using javascript but not sure that'll help.

I've got a fairly simple form

<fieldset>
<div class="formrow">
<label for="contactname">Name:</label>
<input type="text" name="contactname" id="contactname" placeholder="e.g. John Smith" autofocus />
</div>
<div class="formrow">
<label for="email">Email:</label>
<input type="email" name="email" id="email" placeholder="e.g. john@website.com"/>
</div>
<div class="formrow">
<label for="telephone">Telephone:</label>
<input type="tel" name="telephone" id="telephone" placeholder="e.g. (+44) 1242 573311" />
</div>
<div class="formrow">
<label for="message">Message:</label>
<textarea name="message" id="message" placeholder="Your message in here..."></textarea>
</div>
<div class="formrow">
<input type="submit" name="submit" id="submit" value="submit"/>
</div>
</fieldset>


I just don't want double validation on the email and telephone fields.

Any suggestions?
GreyHead 04 Aug, 2011
Hi petersen,

Almost certainly not that way round, the validation process starts on the server and I'm not sure that any useful info is available then. It might be possible to disable it if HTML5 is working. But frankly I've not the first idea how.

Bob
petersen 04 Aug, 2011
hmmm. I've got a solution, but it still loads the javascript files.

Using Modernizr you can check for input types, and then add either an attribute or a class for Chronoforms:


if (Modernizr.inputtypes.email) {
  //Add attribute required="required"
} else {
  //Add class="validate['required','email']"
}


Modernizr comes with YepNope (http://yepnopejs.com/) which you can conditionally load javascript. Perhaps I can implement this in the Chronoform?
GreyHead 05 Aug, 2011
Hi petersen,

Possibly; but you'd need to dig pretty deep in the code to find and remove all the current places that it is loaded.

Bob
petersen 05 Aug, 2011
Sure, maybe something for the future. You can't easily style HTML5 required elements (or rather their messages) at the moment so it's a bit cutting edge.

Thanks anyway.
This topic is locked and no more replies can be posted.