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
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());
}
}
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;
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.
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;
}
}
Bookmarks