Skip to main content

Simple client side validation library

    
Client side validation has never been easy..



Include jQuery and the validator.js 

<script src="http://code.jquery.com/jquery-1.11.3.min.js"></script> <script src="../validate.js"></script>

Simple validation

<form id="myForm"> <label>User Name <span class="red-star">*</span> </label>
<input type="text" name="userName" class="validate-required" />
<br />
<label>Password <span class="red-star">*</span></label>
<input type="password" name="password" class="validate-required" />
<br />
<input type="submit" value="Login" />
</form>
<script type="text/javascript">
$(function () {
validator.initValidationEvents('myForm');
validator.showError = function(element,errorMessage){
$(element).notify(
errorMessage,
{
position:"right",
autoHide: false,
clickToHide: false
}
);
};
validator.hideError = function(element){
$(element).notify('notify-hide');
};
});
</script>

Validating regex


<form id="myForm"> <label>User Name <span class="red-star">*</span> </label>
<input type="text" name="userName"
class="validate-required validate-regexp"
data-regexp='^(ftp|http|https):\/\/[^ "]+$'
data-validate-regexp="Please provide a valid url." />
<br />
<label>Password <span class="red-star">*</span></label>
<input type="password" name="password" class="validate-required" />
<br />
<input type="submit" value="Login" />
</form>
<script type="text/javascript">
/*
You can give any regexp as data-regexp and provide data-validate-regexp as the validation human readable version.
This way you can handle most of the validations out there.
If you need more customization, then you can go for validator.addMethod ( I have provided a separate example for this 04-add-custom-methods.html )
*/
$(function () {
validator.initValidationEvents('myForm');
});
</script>

Validating without a form

<div id="myDiv"> <label>User Name <span class="red-star">*</span> </label> <input type="text" name="userName" class="validate-required" />
<br />
<label>Password <span class="red-star">*</span></label>
<input type="password" name="password" class="validate-required" />
<br />
<input type="button" id="loginButton" value="Login" />
</div>
<script type="text/javascript">
$(function () {
/*
Default case of initValidationEvents method is expecting the page to have a form and a button with type="submit".
But in this case we just have a div container and a button with type="button".
So we are passing a dummy function as an argument to disable submit event handler.
*/
validator.initValidationEvents('myDiv',function(){});
$('#loginButton').click(function(){
if(validator.isValidForm('myDiv')){
alert('your form is valid :D');
} else {
alert('your form is not valid !!!!! :(')
}
})
});
</script>

https://github.com/chanaka3d/validator

Displaying a resource in the WSO2 registry with jaggery


var carbon = require('carbon');
    var server = new carbon.server.Server();
    var options = {
        username: 'admin',
        tenantId: -1234
    };
    var reg = new carbon.registry.Registry(server, options);
    var path = '/_system/es/cartoon001gossiplankanews.png';
    var resource = reg.get(path);
    response.contentType = resource.mediaType;
    print(reg.content(path));

Comments

Anonymous said…
This comment has been removed by the author.

Popular posts from this blog

Projecting HTML Elements on a circle using Javascript

Recently I wonted to create a tree control with a different approach. The requirement was to represent a node tree with main node on the circle center and it's child nodes projected on a circle around it. This is what i wanted. My solution has 5 files prototype.js This is a handy javascript framework. You can download the latest version from there website ( www.prototypejs.org/download ). I am using this to simply handle the onload event in the page. circle.html script.js node-main-left.gif node-sub-left.gif The circle.html, prototype.js and the script.js files are in the root folder and the two images are inside a folder named “images”. This is how the html file looks(circle.html). <html> <head> <script src="prototype.js" type="text/javascript"></script> <script src="script.js" type="text/javascript"></script> <script language="javascript" xml:space...

APIM 3.0 - populate multiple apis - bash script

Created a bash script to create, tag and publish multiple APIs. This is useful to populate data for the landing page. #!/bin/bash # get the URL consumer key clientId=$(curl -k -X POST -H "Authorization: Basic YWRtaW46YWRtaW4=" -H "Content-Type: application/json" -d @payload.json https://localhost:9443/client-registration/v0.14/register | jq -r '.clientId') clientSecret=$(curl -k -X POST -H "Authorization: Basic YWRtaW46YWRtaW4=" -H "Content-Type: application/json" -d @payload.json https://localhost:9443/client-registration/v0.14/register | jq -r '.clientSecret') echo $clientId echo $clientSecret encoded=$(echo -ne $clientId:$clientSecret | base64) echo $encoded # get access token accessToken=$(curl -k -d "grant_type=password&username=admin&password=admin&scope=apim:api_view,apim:api_create" -H "Authorization: Basic $encoded" https://localhost:9443/oauth2/token | jq -r '.access_token'...