2013年9月3日星期二

[转] js 实现完美身份证号有效性验证

js 实现完美身份证号有效性验证

最近需要对身份证合法性进行验证,实名验证是不指望了,不过原来的验证规则太过简单,只是简单的验证了身份证长度,现在业务需要加强下身份证验证规则,网上找到了不少资料,不过都不合偶的心意,无奈只好直接写一个,代码还是用自己的舒服哈
已实现功能:支持15位和18位身份证号,支持地址编码、出生日期、校验位验证 
代码如下:
/*
根据〖中华人民共和国国家标准 GB 11643-1999〗中有关公民身份号码的规定,公民身份号码是特征组合码,由十七位数字本体码和一位数字校验码组成。排列顺序从左至右依次为:六位数字地址码,八位数字出生日期码,三位数字顺序码和一位数字校验码。
    地址码表示编码对象常住户口所在县(市、旗、区)的行政区划代码。
    出生日期码表示编码对象出生的年、月、日,其中年份用四位数字表示,年、月、日之间不用分隔符。
    顺序码表示同一地址码所标识的区域范围内,对同年、月、日出生的人员编定的顺序号。顺序码的奇数分给男性,偶数分给女性。
    校验码是根据前面十七位数字码,按照ISO 7064:1983.MOD 11-2校验码计算出来的检验码。

出生日期计算方法。
    15位的身份证编码首先把出生年扩展为4位,简单的就是增加一个19或18,这样就包含了所有1800-1999年出生的人;
    2000年后出生的肯定都是18位的了没有这个烦恼,至于1800年前出生的,那啥那时应该还没身份证号这个东东,⊙﹏⊙b汗...
下面是正则表达式:
 出生日期1800-2099  (18|19|20)?\d{2}(0[1-9]|1[12])(0[1-9]|[12]\d|3[01])
 身份证正则表达式 /^\d{6}(18|19|20)?\d{2}(0[1-9]|1[12])(0[1-9]|[12]\d|3[01])\d{3}(\d|X)$/i            
 15位校验规则 6位地址编码+6位出生日期+3位顺序号
 18位校验规则 6位地址编码+8位出生日期+3位顺序号+1位校验位
 
 校验位规则     公式:∑(ai×Wi)(mod 11)……………………………………(1)
                公式(1)中: 
                i----表示号码字符从由至左包括校验码在内的位置序号; 
                ai----表示第i位置上的号码字符值; 
                Wi----示第i位置上的加权因子,其数值依据公式Wi=2^(n-1)(mod 11)计算得出。
                i 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1
                Wi 7 9 10 5 8 4 2 1 6 3 7 9 10 5 8 4 2 1
*/
//身份证号合法性验证 
//
支持15位和18位身份证号
//
支持地址编码、出生日期、校验位验证
        function IdentityCodeValid(code) { 
            var city={11:"北京",12:"天津",13:"河北",14:"山西",15:"内蒙古",21:"辽宁",22:"吉林",23:"黑龙江 ",31:"上海",32:"江苏",33:"浙江",34:"安徽",35:"福建",36:"江西",37:"山东",41:"河南",42:"湖北 ",43:"湖南",44:"广东",45:"广西",46:"海南",50:"重庆",51:"四川",52:"贵州",53:"云南",54:"西藏 ",61:"陕西",62:"甘肃",63:"青海",64:"宁夏",65:"新疆",71:"台湾",81:"香港",82:"澳门",91:"国外 "};
            var tip = "";
            var pass= true;
            
            if(!code || !/^\d{6}(18|19|20)?\d{2}(0[1-9]|1[12])(0[1-9]|[12]\d|3[01])\d{3}(\d|X)$/i.test(code)){
                tip = "身份证号格式错误";
                pass = false;
            }
            
           else if(!city[code.substr(0,2)]){
                tip = "地址编码错误";
                pass = false;
            }
            else{
                //18位身份证需要验证最后一位校验位
                if(code.length == 18){
                    code = code.split('');
                    //∑(ai×Wi)(mod 11)
                    //加权因子
                    var factor = [ 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 ];
                    //校验位
                    var parity = [ 1, 0, 'X', 9, 8, 7, 6, 5, 4, 3, 2 ];
                    var sum = 0;
                    var ai = 0;
                    var wi = 0;
                    for (var i = 0; i < 17; i++)
                    {
                        ai = code[i];
                        wi = factor[i];
                        sum += ai * wi;
                    }
                    var last = parity[sum % 11];
                    if(parity[sum % 11] != code[17]){
                        tip = "校验位错误";
                        pass =false;
                    }
                }
            }
            if(!pass) alert(tip);
            return pass;
        }
        var c = '130981199312253466';
       var res= IdentityCodeValid(c);
alert(res); 

注:此文章属懒惰的肥兔原创,版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接:http://www.cnblogs.com/lzrabbit/archive/2011/10/23/2221643.html
若您觉得这篇文章还不错请点击下右下角的推荐,有了您的支持才能激发作者更大的写作热情,非常感谢。
如有问题,可以通过lzrabbit@126.com联系我。

2013年8月19日星期一

[转] Linux 命令行快捷键

Linux 命令行快捷键
FROM: http://coderbee.net/index.php/linux/20130424/41

涉及在linux命令行下进行快速移动光标、命令编辑、编辑后执行历史命令、Bang(!)命令、控制命令等。让basher更有效率。

说明

  • Ctrl – k: 先按住 Ctrl 键,然后再按 k 键;
  • Alt – k: 先按住 Alt 键,然后再按 k 键;
  • M – k:先单击 Esc 键,然后再按 k 键。

移动光标

  • Ctrl – a :移到行首
  • Ctrl – e :移到行尾
  • Ctrl – b :往回(左)移动一个字符
  • Ctrl – f :往后(右)移动一个字符
  • Alt – b :往回(左)移动一个单词
  • Alt – f :往后(右)移动一个单词
  • Ctrl – xx :在命令行尾和光标之间移动
  • M-b :往回(左)移动一个单词
  • M-f :往后(右)移动一个单词

编辑命令

  • Ctrl – h :删除光标左方位置的字符
  • Ctrl – d :删除光标右方位置的字符(注意:当前命令行没有任何字符时,会注销系统或结束终端)
  • Ctrl – w :由光标位置开始,往左删除单词。往行首删
  • Alt – d :由光标位置开始,往右删除单词。往行尾删
  • M – d :由光标位置开始,删除单词,直到该单词结束。
  • Ctrl – k :由光标所在位置开始,删除右方所有的字符,直到该行结束。
  • Ctrl – u :由光标所在位置开始,删除左方所有的字符,直到该行开始。
  • Ctrl – y :粘贴之前删除的内容到光标后。
  • Alt + t :交换光标处和之前两个字符的位置。
  • Alt + . :使用上一条命令的最后一个参数。
  • Ctrl – _ :回复之前的状态。撤销操作。
Ctrl -a + Ctrl -k 或 Ctrl -e + Ctrl -u 或 Ctrl -k + Ctrl -u 组合可删除整行。

Bang(!)命令

  • !! :执行上一条命令。
  • ^foo^bar :把上一条命令里的foo替换为bar,并执行。
  • !wget :执行最近的以wget开头的命令。
  • !wget:p :仅打印最近的以wget开头的命令,不执行。
  • !$ :上一条命令的最后一个参数, 与 Alt - . 和 $_ 相同。
  • !* :上一条命令的所有参数
  • !*:p :打印上一条命令是所有参数,也即 !*的内容。
  • ^abc :删除上一条命令中的abc。
  • ^foo^bar :将上一条命令中的 foo 替换为 bar
  • ^foo^bar^ :将上一条命令中的 foo 替换为 bar
  • !-n :执行前n条命令,执行上一条命令: !-1, 执行前5条命令的格式是: !-5

查找历史命令

  • Ctrl – p :显示当前命令的上一条历史命令
  • Ctrl – n :显示当前命令的下一条历史命令
  • Ctrl – r :搜索历史命令,随着输入会显示历史命令中的一条匹配命令,Enter键执行匹配命令;ESC键在命令行显示而不执行匹配命令。
  • Ctrl – g :从历史搜索模式(Ctrl – r)退出。

控制命令

  • Ctrl – l :清除屏幕,然后,在最上面重新显示目前光标所在的这一行的内容。
  • Ctrl – o :执行当前命令,并选择上一条命令。
  • Ctrl – s :阻止屏幕输出
  • Ctrl – q :允许屏幕输出
  • Ctrl – c :终止命令
  • Ctrl – z :挂起命令

重复执行操作动作


  • M – 操作次数 操作动作 : 指定操作次数,重复执行指定的操作。

2013年8月2日星期五

[转] 实时监听 Input 输入的变化(兼容主流浏览器)

源自

遇到如此需求,首先想到的是change事件,但用过change的都知道只有在input失去焦点时才会触发,并不能满足实时监测的需求,比如监测用户输入字符数。
在经过查阅一番资料后,欣慰的发现firefox等现代浏览器的input有oninput这一属性,可以用三种方式使用它:
1,内嵌元素方式(属性编辑方式)
<input id="test" oninput="console.log('input');" type="text" />
2,句柄编辑方式
document.getElementById('test').oninput=function(){
    console.log('input');
}
3,事件侦听方式(jquery)
$('#test').on('input',function(){
    console.log('input');
})
但是,以上代码仅在除了ie的浏览器大大们里才work,那ie该怎么处理呢? 在ie中有一个属性叫做onpropertychange:
<input id="test" onpropertychange="alert('change');" type="text" />
经过调试后马上就会发现,这个属性是在元素的任何属性变化时都会起作用,包括我们这里所提到的value,但至少是起作用了,那接下来的任务就是筛选出property为value的变化。
document.getElementById('test').attachEvent('onpropertychange',function(e) {
    if(e.propertyName!='value') return;
    $(that).trigger('input');
});
在上面代码中的回调函数中会传入一个参数,为该事件,该事件有很多属性值,搜寻一下可以发现有一个我们很关心的,叫做propertyName,也 就是当前发生变化的属性名称。然后就相当简单了,只要在回调函数中判断一下是否为我们所要的value,是的话就trigger一下‘input’事件。
然后,就可以在主流浏览器中统一用这样的方式来监听‘input’事件了。
$('#test').on('input',function(){
    alert('input');
})
最后贴上完整代码:
$('#test').on('input',function(){
    alert('input');
})
//for ie
if(document.all){
    $('input[type="text"]').each(function() {
        var that=this;
        if(this.attachEvent) {
            this.attachEvent('onpropertychange',function(e) {
                if(e.propertyName!='value') return;
                $(that).trigger('input');
            });
        }
    })
}
参考资料:

2013年7月29日星期一

[转] password strength

password strength


/**
 *
 * @param String $string
 * @return float
 *
 * Returns a float between 0 and 100. The closer the number is to 100 the
 * the stronger password is; further from 100 the weaker the password is.
 */ 
function password_strength($string){
    
$h    0;
    
$size strlen($string);
    foreach(
count_chars($string1) as $v){
        
$p $v $size;
        
$h -= $p log($p) / log(2);
    }
    
$strength = ($h 4) * 100;
    if(
$strength 100){
        
$strength 100;
    }
    return 
$strength;
}
var_dump(password_strength("Correct Horse Battery Staple"));
echo 
"
"
var_dump(password_strength("Super Monkey Ball"));
echo 
"
"
var_dump(password_strength("Tr0ub4dor&3"));
echo 
"
"
var_dump(password_strength("abc123"));
echo 
"
"
var_dump(password_strength("sweet"));

2013年7月26日星期五

[总结] Library not loaded: /opt/local/lib/libiconv.2.dylib 错误解决之法

1. 背景
工作中需要用到 php 的一个图形库的扩展模块 gmagick.
而目前使用的 php 在编译时并没有将 gmagick 静态编译进去,于是通过编译扩展模块来实现.
扩展模块的编译很顺利,但挂在 php 上后发现没有生效.写的简单的脚本测试发现 php error.log 中报如下的错

PHP Warning:  PHP Startup: Unable to load dynamic library '/Users/hy0kl/php/ext/gmagick.so' - dlopen(/Users/hy0kl/php/ext/gmagick.so, 9): Library not loaded: /opt/local/lib/libiconv.2.dylib
  Referenced from: /Users/hy0kl/php/ext/gmagick.so
  Reason: Incompatible library version: gmagick.so requires version 8.0.0 or later, but libiconv.2.dylib provides version 7.0.0 in Unknown on line 0

谷歌后发现,我的机器中有两个版的 libiconv:
$ otool -LD /usr/lib/libiconv.2.dylib
/usr/lib/libiconv.2.dylib:
/usr/lib/libiconv.2.dylib (compatibility version 7.0.0, current version 7.0.0)
/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 159.0.0)

$ otool -LD /opt/local/lib/libiconv.2.dylib
/opt/local/lib/libiconv.2.dylib:
/opt/local/lib/libiconv.2.dylib (compatibility version 8.0.0, current version 8.1.0)
/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 159.1.0)
初步确认是动态的加载顺序没有搞对.
多次尝试 + 不断的 google,终于发现了下面的有用帮助:
https://code.google.com/p/git-osx-installer/issues/detail?id=18

Found out that it wasn't a problem running git as root.

I had this in my personal .profile:
export DYLD_LIBRARY_PATH=/usr/lib
export DYLD_FALLBACK_LIBRARY_PATH=/opt/local/lib:/sw/lib

Hashed it out and everything was fine..
Don't remember why I put it in there in the first place.. :-/


大家千万不要移除或重命名系统 /usr/lib 下的 libiconv,否则系统报各种问题,生不如死.

2. 解决方法:
$ vi ~/.bashrc

DYLD_LIBRARY_PATH=$HOME/local/lib:/usr/local/lib:/usr/local/mysql/lib
export DYLD_LIBRARY_PATH

DYLD_FALLBACK_LIBRARY_PATH=/opt/local/lib:/usr/lib
export DYLD_FALLBACK_LIBRARY_PATH

加入以后内容,通过小改变一下动态库的扫描路径的先后顺序,即可解决了

2013年7月25日星期四

[转] mac port warning

https://gist.github.com/romanr/3192047

Warning: Your developer_dir setting in macports.conf points to a non-existing directory. Since this is known to cause problems, please correct the setting or comment it and let macports auto-discover the correct path.

xcode-select --print-path

sudo nano /opt/local/etc/macports/macports.conf

[转] Building ImageMagick on OSX Lion and can't link libpng

Building ImageMagick on OSX Lion and can't link libpng

Q:
I am trying to build ImageMagick 6.8.0-5 on OSX Lion with support for libpng. I am just using the standard ./configure make make install procedure.
I succeed in compiling when I do not reference libpng.
./configure --with-png=no
make
However, I get an error in make when I try to add libpng support. (libpng-1.5.13)
./configure --with-png=yes
make
The error seems to be a linker error.
/usr/bin/nm: no name list
ld: warning: cannot export hidden symbol _SyncImagePixelCache from magick/.libs/magick_libMagickCore_la-cache.o
ld: warning: cannot export hidden symbol _ResetQuantumState from magick/.libs/magick_libMagickCore_la-quantum.o
Undefined symbols for architecture x86_64:
  "_png_set_check_for_invalid_index", referenced from:
      _WriteOnePNGImage in magick_libMagickCore_la-png.o
      _ReadOnePNGImage in magick_libMagickCore_la-png.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status
make[1]: *** [magick/libMagickCore.la] Error 1
make: *** [all] Error 2
I have not changed any other options to configure.
How can I fix this to add libpng support?

A:
You'll need to tell ImageMagick where to locate libpng. Luckily, libpng ships with a configuration script entitled "libpng-config".
You can get the link-library option form your local libpng install with the --L_opts flag.
libpng-config --L_opts
This will return something like -L/usr/local/lib. Set ImageMagick's LDFLAGS configuration to match your libpng library.
# Use the returned value for the LDFLAGS
./configure LDFLAGS='-L/usr/local/lib'
If libpng-config returns with a message 'command not found', you'll need to ensure your libpng is installed correctly, and can be located in your shell environment. Often, this can be as simple as adding a custom path to your PATH variable.
export PATH="$PATH:/usr/local/bin"

2013年7月23日星期二

git 如何取消跟踪某个文件呢?

$ git rm --cached FILENAME

就可以了.
该操作不会删除原来的文件,同时取消对文件的版本变化跟踪.

2013年7月21日星期日

[转] 解决 ios 使用AdMob遇到 -[GADObjectPrivate changeState:]: unrecognized selector sent to instance 问题

解决 ios 使用AdMob遇到 -[GADObjectPrivate changeState:]: unrecognized selector sent to instance 问题

iso使用AdMob时如遇到-[GADObjectPrivate changeState:]: unrecognized selector sent to instance 0x968c980情况,请在编译选项里linking中的other link flag加上-ObjC,否则蛋碎都找不到原因。

2013年7月16日星期二

[转] add Admob with Cocos2d-x on iOS

add Admob with Cocos2d-x on iOS

1. download admob, setup your account


2. add admob to xcode project. You need:
    - add libGoogleAdmobAds.a to bundle libraries
    - create a group and add admob header file

3.  in AppController.h, add member:     
@interface AppController : NSObject <
UIAccelerometerDelegate, UIAlertViewDelegate, UITextFieldDelegate,UIApplicationDelegate, GADBannerViewDelegate> {
    UIWindow *window;
    RootViewController    *viewController;
    GADBannerView* bannerView_;
}

4. in AppController.mm
    // setup admob
    
    // Create a view of the standard size at the bottom of the screen.
    // Available AdSize constants are explained in GADAdSize.h.
    bannerView_ = [[GADBannerView alloc] initWithAdSize:kGADAdSizeBanner];
    bannerView_.delegate = self;
    // Specify the ad's "unit identifier." This is your AdMob Publisher ID.
    bannerView_.adUnitID = @"YOUR_ID";
    
    // Let the runtime know which UIViewController to restore after taking
    // the user wherever the ad goes and add it to the view hierarchy.
    bannerView_.rootViewController = viewController;
    [viewController.view addSubview:bannerView_];
    
    
    // Initiate a generic request to load it with an ad.
    GADRequest* adRequest = [GADRequest request];
    [bannerView_ loadRequest:adRequest];

    // start game
    cocos2d::CCApplication::sharedApplication()->run();

5. we are not over Yet!!!
    You'll have exceptions if running the app now. You must do this:
(http://stackoverflow.com/questions/12635283/admob-crashes-with-gadobjectprivate-changestate-unrecognized-selector)
You need to add -ObjC to the Other Linker Flags of your application target's build setting:

Click the blue top-level project icon in XCode
Choose your target and go to Build Settings
Under Other Linker Flags add -ObjC for both Release and Debug

Done!!!
Let's make money and go IPO :)