I Am The Freelance Economy

Generalized Custom Validation

September 19, 2012 Leave a comment

Every web development project consist of at-least one form that needs to be validated client side before it gets posted to the server and processed.

There are many libraries out there to validate HTML form, but still sometimes developers need to use custom code for client side validation for some or the other reason.

Let me share with you a generalized function that covers basic validation but with lesser code.

Function includes validation of name, email, password, phone and gender. It can be extended to include many more elements without adding much code.

Image


function is_valid(objVal,valueType)
{
 var ex = "";
 switch(valueType)
 {
 case 'email':
 ex = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
 break;
 case 'phone':
 ex = /^([\+][0-9]{1,3}[ \.\-])?([\(]{1}[0-9]{2,6}[\)])?([0-9 \.\-\/]{3,20})((x|ext|extension)[ ]?[0-9]{1,4})?$/;
 break;
 }
 if(objVal.match(ex))
 {
 return true;
 }
 return false;
}

function general_validation()
{
var error = 0;
var invalid_condition = 0;
//below sequence of form elements must be in reverse order,
//so the focus retains on the element from top to bottom
var form_elements = Array('role_id','phone','password','email','firstname');
var elements_label = Array('Gender','Valid Phone','Password','Valid Email','Full Name');
for (i=0; i < form_elements.length; i++)
{
 invalid_condition = 0;

 if(form_elements[i]=="email" || form_elements[i]=="phone")
 invalid_condition = !is_valid($("#"+form_elements[i]).val(),form_elements[i]);

if($.trim($("#"+form_elements[i]).val())=="" || invalid_condition){
 if(!$("#"+form_elements[i]).hasClass('form-error')){
 var error_msg = 'i] + '_error\">Provide ' + elements_label[i] + '';
 $("#"+form_elements[i]).addClass('form-error').after(error_msg).focus();
 }
 $("#"+form_elements[i]).focus();
 error++;
 } else {
$("#"+form_elements[i]).removeClass('form-error');
 $("#"+form_elements[i]+'_error').remove();
 }
}

if(error){
 return false;
}
return true;
}

You can find the HTML file with forms and style below for download.

Generalized custom validation code

Abort subsequent AJAX calls requested for the same purpose

July 8, 2012 1 comment

Now days the websites are more interactive and thanks to sophisticated technologies like AJAX using which websites can offer interactive and rich user experiences thus adding to customers count.

While using AJAX techniques one must take care that the subsequent request made for the same purpose are blocked otherwise, if user clicks different buttons or links (associated with AJAX call), several times that serve to show some data at common place, then end-user might have a disturbing view with fluctuating data.

If we add little code in each AJAX call, we can block the later calls and please note that we don’t save server’s bandwidth because the call is already dispatched but end-user is not troubled with the fluctuating results.

Let see and understand the code:

//parameter: particularAjaxCallInstance will hold the instance passed to it for respective function.
function abort_consecutive_ajax_calls(particularAjaxCallInstance)
{
//In below condition we check whether the state of the call is not 4 (onreadystatechange) which means the data has not yet arrived, so we can abort it.

if(particularAjaxCallInstance && particularAjaxCallInstance.readyState != 4){
    particularAjaxCallInstance.abort(); //abort ajax call
}
return;
}

var func1Var = ""; //declare a variable for every function you want to avoid subsequent calls

function func1(){

abort_consecutive_ajax_calls(func1Var); //pass that variable which holds the instance of Ajax call and needs to be aborted.

//Below is the variable that is passed as parameter to abort function and is initialized every time a new call is made.

func1Var = $.ajax({
url : 'filename.php',
success: function(data) {  },
});
}
Categories: AJAX, Jquery, linkedin, Technical

Test your PHP skills.

I enjoyed this PHP test very much, there are really quite good question on PHP 5.0.

PHP

Categories: PHP, Technical

Is your password safe online?

  • Was your email account hacked some time back?
  • Can you think of the reason where did you go wrong?
  • Was it your Carelessness or someone’s expertise?

Lets figure out one of the reason and a solution for it.

Many of us have a hard time remembering passwords, so we use the same password for all our personal accounts and social networks, You must have read this obvious thing a thousand times : “It is NOT SAFE to use the same password for all your websites.”

Think of a scenario, where you register on some website say http://www.SiteNotYetCreated.com and used your favorite email account say ‘GMAIL’ (I am a big fan) and same password as that of Gmail account, then later you come to know that site was hacked by some group of people, now there are chances that your email-id and password will be used to login in your email account and all your private stuff is out. (evil laugh)

In the above situation, you must first change your email accounts password right away.

Wait, there are 2 cases now, if that site has stored encrypted format of your password, then you are safe (sigh of relief), but if not then… (evil laugh again). I hope you are now clear about one of the problems, now lets talk about the solution.

Most of the times you need to register on a site just for downloading some document/freeware software, so the chances are less that you are going to visit that again, so why use your favorite password there, use something else.

Now you have a method (not full-proof but effective) to find whether the sites save your password in encrypted format or plain text.

Just after you register on any site, you might get a email of email confirmation, after confirming that, you must visit the site and hit the link which says a similar text like ‘forgot your password?’.

You will be sent a mail either with a link to change/update your password or you might receive the password in plain text. If it’s the latter case, then you know that your account is not safe now.

Its better to delete your account, after you are done with your work on that site, if you want to keep that account for some reason, then you must use such a password which is not used for any of your personal accounts.

Categories: linkedin, Non-Technical

Why I decided to blog

Why I decided to blog?

So finally, I am publishing this blog.

I am web developer by profession, and I have been surviving in this IT industry with the help of online blogs/articles that impart their knowledge for free (Please ignore the ads revenue for time being), but my point here is that they share, they are not keeping it within themselves. This is the thing that makes me happy.

That’s the reason, I have decided to make an effort to write and share my little knowledge with whoever wants to be benefited. I regularly read online articles, I come across many that are hardly commented or shared by people. But still those articles are meaningful, they have a moral to learn and they have a topic to teach.

May be now-a-days people are interested in only that stuff which is funny and that can be shared on Facebook with a LOL.

Every person has some unique skill, some share their knowledge and experience via blog, when we read it and feel helpful, we do our favor by thanking them in comments or by sharing our views. My actual reason for writing this blog is to first motivate myself and then the readers to write blog.

I am planning to write once in a month. I hope you can find at-least 2-3 Hrs in a month for yourself.
The way we take help from online content for guidance on technical and/or non-technical topic,
I think we must return that favor by writing content online by any means about our JOB and life experiences.

Unless we start writing we don’t realize that we have the potential to write down about what we know and share the same with others. So if you get motivated, to write a blog, by reading this article, Please let me know, even I might get inspired too, to write more and write better.

This is a small effort to give back to the Internet community from where I have learned a lot.

PS: Author is new in blogging, so he hardly knows about the ethics for the same, you can criticize if something is wrong, you can appreciate if something is good.

Happy Blogging.

Categories: Non-Technical
Follow

Get every new post delivered to your Inbox.

Join 127 other followers

%d bloggers like this: