无聊却常用的代码生成(简单窗体验证)
# -*- coding: gbk -*-
fields = [
['linkman', '姓名'],
['address', '家庭住址'],
['zip', '邮编'],
['email', '电子邮件'],
['phoneHome', '家庭电话'],
['phoneMobile', '移动电话']
]
print """form validation:
---------------------------------------------------------"""
print """
String.prototype.trim = function() {
return this.replace(/^(\s+)|(\s+)$/g, '');
}
"""
template = """
var obj_%s = document.all('%s');
if (obj_%s.value.trim() == '') {
alert('请输入%s');
obj_%s.focus();
return false;
}
"""
print "function checkForm() {"
for x in fields:
print template % (x[0], x[0], x[0], x[1], x[0])
if x[0] == 'email':
print """
if (obj_email.value.trim().match(/\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/) == null) {
alert('请输入正确的电子邮件');
obj_email.focus();
return false;
}
"""
print """
return true;
}
"""
print """// php:
------------------------------------------------------------""";
for x in fields:
print "$%s = addslashes($_POST['%s']);" % (x[0], x[0])
fields = [
['linkman', '姓名'],
['address', '家庭住址'],
['zip', '邮编'],
['email', '电子邮件'],
['phoneHome', '家庭电话'],
['phoneMobile', '移动电话']
]
print """form validation:
---------------------------------------------------------"""
print """
String.prototype.trim = function() {
return this.replace(/^(\s+)|(\s+)$/g, '');
}
"""
template = """
var obj_%s = document.all('%s');
if (obj_%s.value.trim() == '') {
alert('请输入%s');
obj_%s.focus();
return false;
}
"""
print "function checkForm() {"
for x in fields:
print template % (x[0], x[0], x[0], x[1], x[0])
if x[0] == 'email':
print """
if (obj_email.value.trim().match(/\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/) == null) {
alert('请输入正确的电子邮件');
obj_email.focus();
return false;
}
"""
print """
return true;
}
"""
print """// php:
------------------------------------------------------------""";
for x in fields:
print "$%s = addslashes($_POST['%s']);" % (x[0], x[0])
结果:
>pythonw -u "validate_code_generator.py"
form validation:
---------------------------------------------------------
String.prototype.trim = function() {
return this.replace(/^(\s+)|(\s+)$/g, '');
}
function checkForm() {
var obj_linkman = document.all('linkman');
if (obj_linkman.value.trim() == '') {
alert('请输入姓名');
obj_linkman.focus();
return false;
}
var obj_address = document.all('address');
if (obj_address.value.trim() == '') {
alert('请输入家庭住址');
obj_address.focus();
return false;
}
var obj_zip = document.all('zip');
if (obj_zip.value.trim() == '') {
alert('请输入邮编');
obj_zip.focus();
return false;
}
var obj_email = document.all('email');
if (obj_email.value.trim() == '') {
alert('请输入电子邮件');
obj_email.focus();
return false;
}
if (obj_email.value.trim().match(/\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/) == null) {
alert('请输入正确的电子邮件');
obj_email.focus();
return false;
}
var obj_phoneHome = document.all('phoneHome');
if (obj_phoneHome.value.trim() == '') {
alert('请输入家庭电话');
obj_phoneHome.focus();
return false;
}
var obj_phoneMobile = document.all('phoneMobile');
if (obj_phoneMobile.value.trim() == '') {
alert('请输入移动电话');
obj_phoneMobile.focus();
return false;
}
return true;
}
// php:
------------------------------------------------------------
$linkman = addslashes($_POST['linkman']);
$address = addslashes($_POST['address']);
$zip = addslashes($_POST['zip']);
$email = addslashes($_POST['email']);
$phoneHome = addslashes($_POST['phoneHome']);
$phoneMobile = addslashes($_POST['phoneMobile']);
>Exit code: 0 Time: 0.219
form validation:
---------------------------------------------------------
String.prototype.trim = function() {
return this.replace(/^(\s+)|(\s+)$/g, '');
}
function checkForm() {
var obj_linkman = document.all('linkman');
if (obj_linkman.value.trim() == '') {
alert('请输入姓名');
obj_linkman.focus();
return false;
}
var obj_address = document.all('address');
if (obj_address.value.trim() == '') {
alert('请输入家庭住址');
obj_address.focus();
return false;
}
var obj_zip = document.all('zip');
if (obj_zip.value.trim() == '') {
alert('请输入邮编');
obj_zip.focus();
return false;
}
var obj_email = document.all('email');
if (obj_email.value.trim() == '') {
alert('请输入电子邮件');
obj_email.focus();
return false;
}
if (obj_email.value.trim().match(/\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/) == null) {
alert('请输入正确的电子邮件');
obj_email.focus();
return false;
}
var obj_phoneHome = document.all('phoneHome');
if (obj_phoneHome.value.trim() == '') {
alert('请输入家庭电话');
obj_phoneHome.focus();
return false;
}
var obj_phoneMobile = document.all('phoneMobile');
if (obj_phoneMobile.value.trim() == '') {
alert('请输入移动电话');
obj_phoneMobile.focus();
return false;
}
return true;
}
// php:
------------------------------------------------------------
$linkman = addslashes($_POST['linkman']);
$address = addslashes($_POST['address']);
$zip = addslashes($_POST['zip']);
$email = addslashes($_POST['email']);
$phoneHome = addslashes($_POST['phoneHome']);
$phoneMobile = addslashes($_POST['phoneMobile']);
>Exit code: 0 Time: 0.219