Skip to content

Instantly share code, notes, and snippets.

@felipepodesta
Forked from desamtralized/snippets.js
Created February 28, 2014 15:37
Show Gist options
  • Save felipepodesta/9273146 to your computer and use it in GitHub Desktop.
Save felipepodesta/9273146 to your computer and use it in GitHub Desktop.
//Separate firstname and lastname
var fullName = 'Samuel Barbosa dos Santos';
var nameParts = fullName.split(' ');
var firstName = nameParts.shift();
var lastName = nameParts.join(' ').trim();
//Phone Mask
// jQuery Mask Plugin v1.5.4
// github.com/igorescobar/jQuery-Mask-Plugin
$('.phone').mask('(00)00000-0000');
//CPF Validation
jQuery.fn.validacpf = function(){
this.change(function(){
CPF = $(this).val();
if(!CPF){ return false;}
erro = new String;
cpfv = CPF;
if(cpfv.length == 14 || cpfv.length == 11){
cpfv = cpfv.replace('.', '');
cpfv = cpfv.replace('.', '');
cpfv = cpfv.replace('-', '');
var nonNumbers = /\D/;
if(nonNumbers.test(cpfv)){
erro = "A verificacao de CPF suporta apenas números!";
}else{
if (cpfv == "00000000000" ||
cpfv == "11111111111" ||
cpfv == "22222222222" ||
cpfv == "33333333333" ||
cpfv == "44444444444" ||
cpfv == "55555555555" ||
cpfv == "66666666666" ||
cpfv == "77777777777" ||
cpfv == "88888888888" ||
cpfv == "99999999999") {
erro = "Número de CPF inválido!"
}
var a = [];
var b = new Number;
var c = 11;
for(i=0; i<11; i++){
a[i] = cpfv.charAt(i);
if (i < 9) b += (a[i] * --c);
}
if((x = b % 11) < 2){
a[9] = 0
}else{
a[9] = 11-x
}
b = 0;
c = 11;
for (y=0; y<10; y++) b += (a[y] * c--);
if((x = b % 11) < 2){
a[10] = 0;
}else{
a[10] = 11-x;
}
if((cpfv.charAt(9) != a[9]) || (cpfv.charAt(10) != a[10])){
erro = "Número de CPF inválido.";
}
}
}else{
if(cpfv.length == 0){
return false;
}else{
erro = "Número de CPF inválido.";
}
}
if (erro.length > 0){
$(this).val('');
alert(erro);
setTimeout(function(){$(this).focus();},100);
$(this).focus();
return false;
}
return $(this);
});
}
//EmberView Input with Google Address search autocomplete.
App.AddressAutocompleteView = Ember.TextField.extend({
afterRenderEvent: function () {
var self = this;
var input = this.get('element');
var options = {
types: ['geocode', 'establishment'],
componentRestrictions: {country: 'br'}
};
var autocomplete = new google.maps.places.Autocomplete(input, options);
google.maps.event.addListener(autocomplete, 'place_changed', function () {
console.log(autocomplete.getPlace());
});
}
});
//Ember View After Render Event
Ember.View.reopen({
didInsertElement : function() {
this._super();
Ember.run.scheduleOnce('afterRender', this, this.afterRenderEvent);
},
afterRenderEvent : function() {}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment