The h5Validate does the trick for me. I can't get Webshims to work so that 
wasn't a viable option. It is all-enclusive so it is a more ideal solution than 
h5 but for some reason I have trouble making some js work with things like 
web2py. I've had trouble in the past with Drupal and AbleCommerce having the 
same problems. That is, they work on static pages, but lose something in the 
transfer. The only thing I can think of is that with the header, content, 
footer, et al sections being broken up I'm doing something wrong.

 

Anyway, the web2py method you gave me works like a charm. I got it working no 
problems and for modern browsers, the HTML5 fires first and then on the older 
browsers web2py handles the validation. So that's perfect.

 

One last thing, if you will. Where can you find the programming for the 
response.flash? I would like to keep the behavior of flashing a message if 
there is an error, but I want to define a specific location and have no 
animation whatsoever. The way it works for me out of the box is to shift 
everything down and sort of "roll up" out of nowhere. I can't stand having the 
screen change so I would like to set it to have no animation and just appear. I 
can keep the areas where it is going to appear defined in widths and heights so 
they always exist even if there is no content but the rolling effect stills 
shifts everything.

 

hopefully I explained that last part right. Thanks for your help, Anthony!

 

From: [email protected] [mailto:[email protected]] On Behalf Of 
Anthony
Sent: Wednesday, April 11, 2012 5:17 PM
To: [email protected]
Subject: [web2py] Re: Mimicking HTML5 pattern attribute for inputs

 

If you want to stick with client side validation, you might also consider a 
Javascript polyfill library like Webshims Lib 
(http://afarkas.github.com/webshim/demos/), which lets you use HTML5 features 
in older browsers. Also note that although client-side validation improves the 
user experience, it does not protect against malicious attacks, so you may 
still want some server-side validation in addition.

 

Anthony

On Wednesday, April 11, 2012 4:21:35 PM UTC-4, RKS wrote:

I'm looking to validate all of my forms with HTML5 and the pattern attribute, 
but as always, nothing is perfect on the internet and I still need a backup to 
catch those users who do not use HTML5 friendly browsers.

I'm having trouble finding exact representations of the regular expressions in 
the handbook so if you know, I'd appreciate some help. I have written the code 
in two ways, the HTML way and the HTML helper way (see below) and the HTML5 
works and validates in modern browsers except IE as expected but I can't 
translate them to python.

An example, you will see below, is the expression 
pattern="[A-F0-9]{11}|[A-F0-9]{14}"
This effectively forces an input to only contain uppercase letters of A-F and 
numbers 0-9. It also ensures the length is exactly 11 characters or 14. So how 
would I use this in web2py? IS_LENGTH seems to accept only a range from my 
tests and so far I've only found IS_ALPHANUMERIC to control what characters are 
accepted.

Please seem the form below:

HTML:

<form id="activate_form" method="post" action="">
        <label for="meid">MSIE/ESN <sup>*</sup></label>
        <input name="meid" pattern="[A-Fa-f0-9]{11}|[A-Fa-f0-9]{14}" 
placeholder="MEID/ESN" required />
        <br />
        <label for="zip">Zip Code <sup>*</sup></label>
        <input type="number" name="zip" pattern="{5}" placeholder="Zip Code" 
required />
        <br />
        <br />
        <br /><br />
        <input type="button" name="cancel" value="Cancel" 
onClick="history.go(-1);return true;" />
        <input type="submit" name="submit" value="Activate" />
           <p class="small"><sup>*</sup> denotes a required field.
    </form>



HTML Helpers:

form=FORM(LABEL('MEID/ESN 
',(SPAN('*'))),INPUT(_name='meid',_pattern="[A-F0-9]{11}|[A-F0-9]{14}",_placeholder='MEID/ESN',_required='required',_title="The
 MEID/ESN number only contains 11 or 13 characters, the letters A-F, and the 
numbers 0-9.",requires=[IS_LENGTH(11|14),IS_NOT_EMPTY()], 
_onblur="this.checkValidity();"),BR(),LABEL('ZIP CODE 
',(SPAN('*'))),INPUT(_name='zip',_type='number',_pattern="[0-9]{5}",_placeholder='Zip
 Code',_required='required',_title="We only required the five character zip 
code.",requires=IS_NOT_EMPTY()),BR(),BR(),BR(),BR(),INPUT(_type='button',_name='cancel',_value='Cancel',_onclick="history.go(-1);return
 
true;"),INPUT(_type='submit',_name='submit',_value='Activate'),_method='post',_id='activate_form')
    if form.accepts(request,session):
        response.flash = 'Form accepted'
     #   redirect(URL('next'))
    elif form.errors:
        response.flash = 'Form has errors'
    return dict(form=form)



Thanks.

Reply via email to