Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- if (!empty($_POST)){
- header('Cache-Control: no-cache, must-revalidate');
- header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
- header('Content-type: application/json');
- die(json_encode($_POST));
- }
- ?><html>
- <head>
- <title>Auto-validating Form</title>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
- <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" type="text/javascript" ></script>
- <style>
- #registration {
- margin-top:10px;
- }
- #registration label{
- position:absolute;
- }
- #registration>div{
- height:60px;
- }
- #registration>div>input, #registration>div>textarea{
- position:absolute;
- margin-left: 120px;
- width:200px;
- }
- #registration .submit{
- display:block;
- margin-left:100px;
- width:100px;
- }
- #registration .messages{
- position:absolute;
- display:block;
- margin-left: 330px;
- }
- #registration .messages>.error{
- background: #FEF1EC;
- color: #CD0A0A;
- }
- #registration .messages>.notice{
- background: #FBF9EE;
- color: #363636;
- }
- #registration .messages>div{
- display:none;
- padding:5px;
- width:500px;
- height:40px;
- }
- #mightyResponse{
- display:none;
- }
- </style>
- </head>
- <body>
- <form id="registration" method="post" action="">
- <div>
- <label for="name">Name</label>
- <input type="text" name="name" />
- <div class="messages">
- <div class="error not" title="/^[a-zàèòùì \.\-]{3,}$/i">I bet your name is longer than 3 characters and contains only letters, dots(.) and dashes(-)</div>
- <div class="notice" title="/^[a-zàèòùì \.\-]{3,5}$/i">Aye there, shorty name</div>
- </div>
- </div>
- <div>
- <label for="surname">Surname</label>
- <input type="text" name="surname" />
- <div class="messages">
- <div class="error not" title="/^[a-zàèòùì \.\-]{3,}$/i">I bet your surname is longer than 3 characters and contains only letters, dots(.) and dashes(-)</div>
- <div class="notice" title="/^[a-zàèòùì \.\-]{8,}$/i">Hope your surname doesn't have to COMPENSATE anything</div>
- </div>
- </div>
- <div>
- <label for="email">Email</label>
- <input type="text" name="email" />
- <div class="messages">
- <div class="error not" title="/^[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,4}$/i">
- Your email is not valid (or my regex doesn't match it:
- I'm too <a href="http://www.mailinator.com">lazy</a> to copy that loooong RFC 2822 implementation just to support you boring 1%) </div>
- </div>
- </div>
- <div>
- <label for="description">Description</label>
- <textarea name="description"></textarea>
- <div class="messages">
- <div class="error" title="/^$/i">Please, just spare one letter for the poors!</div>
- </div>
- </div>
- <div>
- <label for="spam">Interested in my <br />crappy mailing list?</label>
- <input type="checkbox" name="spam" />
- <div class="messages">
- <div class="notice" title="/^false$/i">Too bad! You should!!!</div>
- </div>
- </div>
- <br />
- <input type="submit" value="Register" class="submit" />
- </form>
- <div id="mightyResponse">
- <h2>Your mighty server replied back:</h2>
- <pre></pre>
- </div>
- <script>
- (function ($){
- $.fn.anotherLameFormPlugin = function (o){
- var _this = this,jQuery
- validate = function(){
- var status = true;
- _this.find('input, textarea').each(function(){
- var element = $(this),
- messages = element.next('.messages').children('div');
- if (messages.length){
- var text, results = [];
- if (element.is(':checkbox'))
- text = element.is(':checked').toString();
- else
- text = element.val();
- messages.each(function(){
- var message = $(this),
- regex = message.attr('title'),
- eor = regex.lastIndexOf('/'),
- modifier = regex.substr(eor+1);
- regex = regex.substr(1, eor-1);
- regex = new RegExp(regex, modifier);
- message.hide();
- if (regex.test(text) === !message.hasClass('not')){
- if (message.hasClass('error')){
- status = false;
- }
- message.fadeIn();
- }
- });
- }
- });
- return status;
- }
- this.submit(function(){
- if (!validate())
- return false;
- var method = $.get;
- if (_this.attr('method').toLowerCase()=='post')
- method = $.post;
- method(_this.action, _this.serialize(), o.callback);
- return false;
- });
- return this;
- };
- }(jQuery));
- $('#registration').anotherLameFormPlugin({
- callback: function(data){
- $("#mightyResponse")
- .fadeIn()
- .children('pre').html(JSON.stringify(data, null, '\t'));
- }
- })
- </script>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement