www.webdeveloper.com
Results 1 to 3 of 3

Thread: Post Code Validation Problem

  1. #1
    Join Date
    Jan 2011
    Posts
    2

    Exclamation Post Code Validation Problem

    Dear Friends,

    My name is Şükrü. I am writing you from Turkey. Firstly thanks your nice projects. But I have a problem about post code validation. In the example developer used Uk Post codes but my country use only 5 numbers. Turkey's post code start 01000 and end 81950. (
    Turkey's post codes examples : (http://www.ptt.gov.tr/tr/interaktif/pkodu1.zip ) For example, if user write 00600 or 81960 than the system should give us a error.Please help me, how do I ?

    Best regards.

    ---------------------------------------------------------------------------------------
    Java Script Codes Here


    /**************************************************************

    Script : Validate
    Version : 2.1
    Authors : Samuel Birch
    Desc : Form validation
    Licence : Open Source MIT Licence

    **************************************************************/

    var Validate = new Class({

    getOptions: function(){
    return {
    validateOnBlur: true,
    errorClass: 'error',
    errorMsgClass: 'errorMessage',
    dateFormat: 'dd/MM/yy',
    onFail: Class.empty,
    onSuccess: false,
    showErrorsInline: true,
    label: 'Please wait...'
    };
    },

    initialize: function(form, options){
    this.setOptions(this.getOptions(), options);

    this.form = $(form);
    this.elements = this.form.getElements('.required');

    this.list = [];

    this.elements.each(function(el,i){
    if(this.options.validateOnBlur){
    el.addEvent('blur', this.validate.bind(this, el));
    }
    }.bind(this));

    this.form.addEvent('submit', function(e){
    var event = new Event(e);
    var doSubmit = true;
    this.elements.each(function(el,i){
    if(! this.validate(el)){
    event.stop();
    doSubmit = false
    this.list.include(el);
    }else{
    this.list.remove(el);
    }
    }.bind(this));

    if(doSubmit){
    if(this.options.onSuccess){
    event.stop();
    this.options.onSuccess(this.form);
    }else{
    this.form.getElement('input[type=submit]').setProperty('value',this.options.label);
    }
    }else{
    this.options.onFail(this.getList());
    }

    }.bind(this));

    },

    getList: function(){
    var list = new Element('ul');
    this.list.each(function(el,i){
    if(el.title != ''){
    var li = new Element('li').injectInside(list);
    new Element('label').setProperty('for', el.id).setText(el.title).injectInside(li);
    }
    });
    return list;
    },

    validate: function(el){
    var valid = true;
    this.clearMsg(el);

    switch(el.type){
    case 'text':
    case 'textarea':
    case 'select-one':
    if(el.value != ''){
    if(el.hasClass('email')){
    var regEmail = /^[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/;
    if(el.value.toUpperCase().match(regEmail)){
    valid = true;
    }else{
    valid = false;
    this.setMsg(el, 'Please enter a valid email address');
    }
    }

    if(el.hasClass('number')){
    var regNum = /[-+]?[0-9]*\.?[0-9]+/;
    if(el.value.match(regNum)){
    valid = true;
    }else{
    valid = false;
    this.setMsg(el, 'Please enter a valid number');
    }
    }

    if(el.hasClass('postcode')){
    var regPC = /^([Gg][Ii][Rr] 0[Aa]{2})|((([A-Za-z][0-9]{1,2})|(([A-Za-z][A-Ha-hJ-Yj-y][0-9]{1,2})|(([A-Za-z][0-9][A-Za-z])|([A-Za-z][A-Ha-hJ-Yj-y][0-9]?[A-Za-z])))) [0-9][A-Za-z]{2})$/
    if(el.value.match(regPC)){
    valid = true;
    }else{
    valid = false;
    this.setMsg(el, 'Please enter a valid postcode');
    }
    }

    if(el.hasClass('date')){
    var d = Date.parseExact(el.value, this.options.dateFormat);
    if(d != null){
    valid = true;
    }else{
    valid = false;
    this.setMsg(el, 'Please enter a valid date in the format: '+this.options.dateFormat.toLowerCase());
    }
    }

    }else{
    valid = false;
    this.setMsg(el);
    }
    break;

    case 'checkbox':
    if(!el.checked){
    valid = false;
    this.setMsg(el);
    }else{
    valid = true;
    }
    break;

    case 'radio':
    var rad = $A(this.form[el.name]);
    var ok = false;
    rad.each(function(e,i){
    if(e.checked){
    ok = true;
    }
    });
    if(!ok){
    valid = false;
    this.setMsg(rad.getLast(), 'Please select an option');
    }else{
    valid = true;
    this.clearMsg(rad.getLast());
    }
    break;

    }
    return valid;
    },

    setMsg: function(el, msg){
    if(msg == undefined){
    msg = el.title;
    }
    if(this.options.showErrorsInline){
    if(el.error == undefined){
    el.error = new Element('span').addClass(this.options.errorMsgClass).setText(msg).injectAfter(el);
    }else{
    el.error.setText(msg);
    }
    el.addClass(this.options.errorClass);
    }
    },

    clearMsg: function(el){
    el.removeClass(this.options.errorClass);
    if(el.error != undefined){
    el.error.remove();
    el.error = undefined;
    }
    }

    });

    Validate.implement(new Options);
    Validate.implement(new Events);


    /*************************************************************/

  2. #2
    Join Date
    Dec 2002
    Location
    St. Louis, MO, USA
    Posts
    1,582
    To validate each field, individually, immediately upon blur can get annoying. I suggest creating a function that sets a var warn=""; and append error messages to it as each field is validated, then if warn still equals "" go ahead and submit; otherwise, alert the warn variable and return false.

    You can use a combination of regex and standard conditional to check for postal code validation.

    First, check to make sure it's all numbers and is exactly five digits in length.
    Code:
    var warn = "";
    var df = document.[form name];
    var reMask = /^\d{5}$/; // RegEx mask, five digits only
    if(!reMask.test(df.[postal code field name].value)) {
      warn += "Postal Code is not in proper format.\n";
      }
    else {
      if((df.[postal code field name].value < 1000) || (df.[postal code field name].value > 81950)) {
        warn += "Postal Code is not between 01000 and 81950.\n";
        }
      }
    if(warn = "") { return true; }
    else { alert(warn); return false; }
    In the form onSubmit, put "return [function name]();" in order to validate.

    ^_^

  3. #3
    Join Date
    Jan 2011
    Posts
    2

    Thanks alot

    Quote Originally Posted by WolfShade View Post
    To validate each field, individually, immediately upon blur can get annoying. I suggest creating a function that sets a var warn=""; and append error messages to it as each field is validated, then if warn still equals "" go ahead and submit; otherwise, alert the warn variable and return false.

    You can use a combination of regex and standard conditional to check for postal code validation.

    First, check to make sure it's all numbers and is exactly five digits in length.
    Code:
    var warn = "";
    var df = document.[form name];
    var reMask = /^\d{5}$/; // RegEx mask, five digits only
    if(!reMask.test(df.[postal code field name].value)) {
      warn += "Postal Code is not in proper format.\n";
      }
    else {
      if((df.[postal code field name].value < 1000) || (df.[postal code field name].value > 81950)) {
        warn += "Postal Code is not between 01000 and 81950.\n";
        }
      }
    if(warn = "") { return true; }
    else { alert(warn); return false; }
    In the form onSubmit, put "return [function name]();" in order to validate.

    ^_^

    Dear WolfShade,

    Firstly thanks your interest and examples. I changed your code as follow ;

    if(el.hasClass('postcode')){
    var reMask = /^\d{5}$/; // RegEx mask, five digits only
    if((el.value.match(reMask) < 1000) || (el.value.match(reMask) > 81950)) {
    valid = false;
    this.setMsg(el, '5 haneli posta kodunu giriniz');
    }else{
    valid = true;
    }
    }
    Now form is working and there is no problem.

    Thanks again.
    Best regards.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles