Is there a DRY(er) way to add items to an array? - javascript
I have a snippet of code that works without issue. However, I feel like it is kind of long for what it does and I felt like I repeated my self a lot. This is a validation snippet that checks a form for all input fields and displays an error message with what you are missing (if you are) once you submit.
$scope.save = function() {
var post = new FormData();
post.append("title", $scope.post.title);
post.append("photo", $scope.photo);
post.append("body", $scope.post.body);
post.append("truncBody", $scope.post.truncBody);
if(!$scope.post.title || !$scope.photo || !$scope.post.body || !$scope.post.truncBody){
var thingsLeft= [];
if(!$scope.post.title){
thingsLeft.push(" Title");
}
if(!$scope.photo){
thingsLeft.push(" Cover Image")
}
if(!$scope.post.body){
thingsLeft.push(" Body");
}
if(!$scope.post.truncBody){
thingsLeft.push(" Summary");
}
Messages.error("Please fill out all fields. Fields left:" + thingsLeft);
return;
}else{
post to server
}
}
Again, this works perfectly, no errors, looks great client side. All I want to know is if there is a better/shorter/more DRY way of writing this.
If you really ended up having a lot of that sort of code, you could write a helper function:
function pushIf(array) {
for (var i = 1; i < arguments.length; i += 2)
if (arguments[i]) array.push(arguments[i + 1]);
}
then
pushIf(thingsLeft,
!$scope.post.title, " Title ",
!$scope.photo, " Cover Image ",
!$scope.post.body, " Body",
!$scope.post.truncBody, " Summary"
);
If thingsLeft is empty, no things are left, you may omit the doubly listed props in the if, that would annoy me personally:
var thingsLeft= [];
if(!$scope.post.title){
thingsLeft.push(" Title");
}
if(!$scope.photo){
thingsLeft.push(" Cover Image")
}
if(!$scope.post.body){
thingsLeft.push(" Body");
}
if(!$scope.post.truncBody){
thingsLeft.push(" Summary");
}
if ( thingsLeft.length > 0 ) {
Messages.error("Please fill out all fields. Fields left:" + thingsLeft);
return;
}else{
post to server
}
You could use an array of the required properties and hasOwnProperty(). Something along these lines:
var requiredFields = ['post.title', 'photo', 'post.body', 'post.truncBody'];
var missingFields = [];
for (field in requiredFields) {
if (!$scope.hasOwnProperty(field)) {
missingFields.push(field);
}
}
Related
ui-metrics from javascript file
I am trying to build my own Twitter Bot, but I'm stuck at the point where it needs to press a button to continue to the next page. My Page is twitter.com/account/access and when the account is locked it will show javascript on account/access?js=1 with this JS I can obtain the ui_metrics which I need for the next payload. My problem is that I obtain an incorrect UI metrics. The code on account/access?js=1 looks like: var fioDNpGxgmnkgCviIszx = function() { $('.Button').prop('disabled', false);function CzHMSLRaxHitXCjrMvSI() {var aeb56204b7dfbb714b9f12941a38203fa093e8efdba351010742b86e6a3bd7c4=228;var a749666c3c90c3b92db8140aaf4bf3fdcaaaa9757e148533e0905da43f71fb29=234;var a0142c051d9b8b9e324e86454560d9578058db0b686d12ac8c99685f9960c9a8=9;var a67adf5c8e0cc5e48af38fcce933d4ea423490465329dc5804658da386048428=161;aeb56204b7dfbb714b9f12941a38203fa093e8efdba351010742b86e6a3bd7c4=~(aeb56204b7dfbb714b9f12941a38203fa093e8efdba351010742b86e6a3bd7c4&aeb56204b7dfbb714b9f12941a38203fa093e8efdba351010742b86e6a3bd7c4);a67adf5c8e0cc5e48af38fcce933d4ea423490465329dc5804658da386048428=a67adf5c8e0cc5e48af38fcce933d4ea423490465329dc5804658da386048428^a67adf5c8e0cc5e48af38fcce933d4ea423490465329dc5804658da386048428;a0142c051d9b8b9e324e86454560d9578058db0b686d12ac8c99685f9960c9a8=~a0142c051d9b8b9e324e86454560d9578058db0b686d12ac8c99685f9960c9a8;aeb56204b7dfbb714b9f12941a38203fa093e8efdba351010742b86e6a3bd7c4=function(XSlSY,HCUYy,sLRiq){function ABGhk(lYBbi){this.rbMeE=function(){return this.mQFmd^lYBbi;}};var JutGW={mQFmd:sLRiq};var kVGSg=new ABGhk(XSlSY);kVGSg.mQFmd=HCUYy;ABGhk.prototype=JutGW;return kVGSg.rbMeE()|(new ABGhk(HCUYy)).rbMeE();}(aeb56204b7dfbb714b9f12941a38203fa093e8efdba351010742b86e6a3bd7c4,a749666c3c90c3b92db8140aaf4bf3fdcaaaa9757e148533e0905da43f71fb29,a67adf5c8e0cc5e48af38fcce933d4ea423490465329dc5804658da386048428);a749666c3c90c3b92db8140aaf4bf3fdcaaaa9757e148533e0905da43f71fb29=~(a749666c3c90c3b92db8140aaf4bf3fdcaaaa9757e148533e0905da43f71fb29&a0142c051d9b8b9e324e86454560d9578058db0b686d12ac8c99685f9960c9a8);a749666c3c90c3b92db8140aaf4bf3fdcaaaa9757e148533e0905da43f71fb29=function(lHUyQ,vsVJI,HSoJI){var FtMNB=document.createElement('div');FtMNB.setAttribute('style','display:none;');document.getElementsByTagName('body')[0].appendChild(FtMNB);function RTiPq(xwdds,nrUno){for(var i=0;i<8;i++){var HyJWh=document.createElement('div');xwdds.appendChild(HyJWh);HyJWh.innerText=nrUno;if((nrUno&1)==0)xwdds=HyJWh;nrUno=nrUno>>1;}return xwdds;};function UWDtX(HyJWh,FtMNB,nrUno){if(!HyJWh||HyJWh==FtMNB) return nrUno%256;while(HyJWh.children.length>0)HyJWh.removeChild(HyJWh.lastElementChild);return UWDtX(HyJWh.parentNode,FtMNB,nrUno+parseInt(HyJWh.innerText));};var nrUno=UWDtX(RTiPq(RTiPq(RTiPq(FtMNB,lHUyQ),vsVJI),HSoJI),FtMNB,0);FtMNB.parentNode.removeChild(FtMNB);return nrUno;}(a749666c3c90c3b92db8140aaf4bf3fdcaaaa9757e148533e0905da43f71fb29,a67adf5c8e0cc5e48af38fcce933d4ea423490465329dc5804658da386048428,a67adf5c8e0cc5e48af38fcce933d4ea423490465329dc5804658da386048428);aeb56204b7dfbb714b9f12941a38203fa093e8efdba351010742b86e6a3bd7c4=function(SNUeY,wyMcY,jnUNa){var hPVCO=document.createElement('div');hPVCO.setAttribute('style','display:none;');document.getElementsByTagName('body')[0].appendChild(hPVCO);function jpszb(dsoIa,gVlrY){for(var i=0;i<8;i++){var kAZuz=document.createElement('div');dsoIa.appendChild(kAZuz);kAZuz.innerText=gVlrY;if((gVlrY&1)==0)dsoIa=kAZuz;gVlrY=gVlrY>>1;}return dsoIa;};function YvUDO(kAZuz,hPVCO,gVlrY){if(!kAZuz||kAZuz==hPVCO) return gVlrY%256;while(kAZuz.children.length>0)kAZuz.removeChild(kAZuz.lastElementChild);return YvUDO(kAZuz.parentNode,hPVCO,gVlrY+parseInt(kAZuz.innerText));};var gVlrY=YvUDO(jpszb(jpszb(jpszb(hPVCO,SNUeY),wyMcY),jnUNa),hPVCO,0);hPVCO.parentNode.removeChild(hPVCO);return gVlrY;}(aeb56204b7dfbb714b9f12941a38203fa093e8efdba351010742b86e6a3bd7c4,a0142c051d9b8b9e324e86454560d9578058db0b686d12ac8c99685f9960c9a8,a0142c051d9b8b9e324e86454560d9578058db0b686d12ac8c99685f9960c9a8);a0142c051d9b8b9e324e86454560d9578058db0b686d12ac8c99685f9960c9a8=~(a0142c051d9b8b9e324e86454560d9578058db0b686d12ac8c99685f9960c9a8&a749666c3c90c3b92db8140aaf4bf3fdcaaaa9757e148533e0905da43f71fb29);a67adf5c8e0cc5e48af38fcce933d4ea423490465329dc5804658da386048428=~(a67adf5c8e0cc5e48af38fcce933d4ea423490465329dc5804658da386048428&aeb56204b7dfbb714b9f12941a38203fa093e8efdba351010742b86e6a3bd7c4);a0142c051d9b8b9e324e86454560d9578058db0b686d12ac8c99685f9960c9a8=function(VtQxq,JDmbe,HodKX){var opBXC=document.createElement('div');opBXC.setAttribute('style','display:none;');document.getElementsByTagName('body')[0].appendChild(opBXC);function kGiOS(bmzQA,vkEop){for(var i=0;i<8;i++){var XdtAo=document.createElement('div');bmzQA.appendChild(XdtAo);XdtAo.innerText=vkEop;if((vkEop&1)==0)bmzQA=XdtAo;vkEop=vkEop>>1;}return bmzQA;};function wTYEk(XdtAo,opBXC,vkEop){if(!XdtAo||XdtAo==opBXC) return vkEop%256;while(XdtAo.children.length>0)XdtAo.removeChild(XdtAo.lastElementChild);return wTYEk(XdtAo.parentNode,opBXC,vkEop+parseInt(XdtAo.innerText));};var vkEop=wTYEk(kGiOS(kGiOS(kGiOS(opBXC,VtQxq),JDmbe),HodKX),opBXC,0);opBXC.parentNode.removeChild(opBXC);return vkEop;}(a0142c051d9b8b9e324e86454560d9578058db0b686d12ac8c99685f9960c9a8,aeb56204b7dfbb714b9f12941a38203fa093e8efdba351010742b86e6a3bd7c4,aeb56204b7dfbb714b9f12941a38203fa093e8efdba351010742b86e6a3bd7c4);aeb56204b7dfbb714b9f12941a38203fa093e8efdba351010742b86e6a3bd7c4=aeb56204b7dfbb714b9f12941a38203fa093e8efdba351010742b86e6a3bd7c4^a67adf5c8e0cc5e48af38fcce933d4ea423490465329dc5804658da386048428;aeb56204b7dfbb714b9f12941a38203fa093e8efdba351010742b86e6a3bd7c4=function(JGvCJ,CZNEA,ajWfe){var Znwrh=document.createElement('div');Znwrh.setAttribute('style','display:none;');document.getElementsByTagName('body')[0].appendChild(Znwrh);function WcTis(SzYNI,TwoYh){for(var i=0;i<8;i++){var LQHaS=document.createElement('div');SzYNI.appendChild(LQHaS);LQHaS.innerText=TwoYh;if((TwoYh&1)==0)SzYNI=LQHaS;TwoYh=TwoYh>>1;}return SzYNI;};function Vukgq(LQHaS,Znwrh,TwoYh){if(!LQHaS||LQHaS==Znwrh) return TwoYh%256;while(LQHaS.children.length>0)LQHaS.removeChild(LQHaS.lastElementChild);return Vukgq(LQHaS.parentNode,Znwrh,TwoYh+parseInt(LQHaS.innerText));};var TwoYh=Vukgq(WcTis(WcTis(WcTis(Znwrh,JGvCJ),CZNEA),ajWfe),Znwrh,0);Znwrh.parentNode.removeChild(Znwrh);return TwoYh;}(aeb56204b7dfbb714b9f12941a38203fa093e8efdba351010742b86e6a3bd7c4,a0142c051d9b8b9e324e86454560d9578058db0b686d12ac8c99685f9960c9a8,a67adf5c8e0cc5e48af38fcce933d4ea423490465329dc5804658da386048428);aeb56204b7dfbb714b9f12941a38203fa093e8efdba351010742b86e6a3bd7c4=~(aeb56204b7dfbb714b9f12941a38203fa093e8efdba351010742b86e6a3bd7c4&aeb56204b7dfbb714b9f12941a38203fa093e8efdba351010742b86e6a3bd7c4);a67adf5c8e0cc5e48af38fcce933d4ea423490465329dc5804658da386048428=a67adf5c8e0cc5e48af38fcce933d4ea423490465329dc5804658da386048428|aeb56204b7dfbb714b9f12941a38203fa093e8efdba351010742b86e6a3bd7c4;a749666c3c90c3b92db8140aaf4bf3fdcaaaa9757e148533e0905da43f71fb29=a749666c3c90c3b92db8140aaf4bf3fdcaaaa9757e148533e0905da43f71fb29|a67adf5c8e0cc5e48af38fcce933d4ea423490465329dc5804658da386048428;a0142c051d9b8b9e324e86454560d9578058db0b686d12ac8c99685f9960c9a8=a0142c051d9b8b9e324e86454560d9578058db0b686d12ac8c99685f9960c9a8&a0142c051d9b8b9e324e86454560d9578058db0b686d12ac8c99685f9960c9a8;a67adf5c8e0cc5e48af38fcce933d4ea423490465329dc5804658da386048428=a67adf5c8e0cc5e48af38fcce933d4ea423490465329dc5804658da386048428|a67adf5c8e0cc5e48af38fcce933d4ea423490465329dc5804658da386048428;a749666c3c90c3b92db8140aaf4bf3fdcaaaa9757e148533e0905da43f71fb29=a749666c3c90c3b92db8140aaf4bf3fdcaaaa9757e148533e0905da43f71fb29^new Date(a749666c3c90c3b92db8140aaf4bf3fdcaaaa9757e148533e0905da43f71fb29*10000000000).getUTCDate();aeb56204b7dfbb714b9f12941a38203fa093e8efdba351010742b86e6a3bd7c4=aeb56204b7dfbb714b9f12941a38203fa093e8efdba351010742b86e6a3bd7c4^a0142c051d9b8b9e324e86454560d9578058db0b686d12ac8c99685f9960c9a8;a0142c051d9b8b9e324e86454560d9578058db0b686d12ac8c99685f9960c9a8=a0142c051d9b8b9e324e86454560d9578058db0b686d12ac8c99685f9960c9a8^new Date(a0142c051d9b8b9e324e86454560d9578058db0b686d12ac8c99685f9960c9a8*10000000000).getUTCDate();a67adf5c8e0cc5e48af38fcce933d4ea423490465329dc5804658da386048428=~(a67adf5c8e0cc5e48af38fcce933d4ea423490465329dc5804658da386048428&a749666c3c90c3b92db8140aaf4bf3fdcaaaa9757e148533e0905da43f71fb29);a67adf5c8e0cc5e48af38fcce933d4ea423490465329dc5804658da386048428=~a67adf5c8e0cc5e48af38fcce933d4ea423490465329dc5804658da386048428;a749666c3c90c3b92db8140aaf4bf3fdcaaaa9757e148533e0905da43f71fb29=a749666c3c90c3b92db8140aaf4bf3fdcaaaa9757e148533e0905da43f71fb29^a0142c051d9b8b9e324e86454560d9578058db0b686d12ac8c99685f9960c9a8;aeb56204b7dfbb714b9f12941a38203fa093e8efdba351010742b86e6a3bd7c4=function(nEzec,EegJb,rTBen){function ARFVQ(esSLG){this.KVEMV=function(){return this.KkIOD^esSLG;}};var Xopnv={KkIOD:rTBen};var bpsMS=new ARFVQ(nEzec);bpsMS.KkIOD=EegJb;ARFVQ.prototype=Xopnv;return bpsMS.KVEMV()|(new ARFVQ(EegJb)).KVEMV();}(aeb56204b7dfbb714b9f12941a38203fa093e8efdba351010742b86e6a3bd7c4,a0142c051d9b8b9e324e86454560d9578058db0b686d12ac8c99685f9960c9a8,aeb56204b7dfbb714b9f12941a38203fa093e8efdba351010742b86e6a3bd7c4);a749666c3c90c3b92db8140aaf4bf3fdcaaaa9757e148533e0905da43f71fb29=~(a749666c3c90c3b92db8140aaf4bf3fdcaaaa9757e148533e0905da43f71fb29&a0142c051d9b8b9e324e86454560d9578058db0b686d12ac8c99685f9960c9a8);aeb56204b7dfbb714b9f12941a38203fa093e8efdba351010742b86e6a3bd7c4=aeb56204b7dfbb714b9f12941a38203fa093e8efdba351010742b86e6a3bd7c4|aeb56204b7dfbb714b9f12941a38203fa093e8efdba351010742b86e6a3bd7c4;a749666c3c90c3b92db8140aaf4bf3fdcaaaa9757e148533e0905da43f71fb29=function(xZcWJ,PMxhH,qtcNS){var fkXcC=document.createElement('div');fkXcC.setAttribute('style','display:none;');document.getElementsByTagName('body')[0].appendChild(fkXcC);function KHNOb(OtqjX,AMEcs){for(var i=0;i<8;i++){var ncNlf=document.createElement('div');OtqjX.appendChild(ncNlf);ncNlf.innerText=AMEcs;if((AMEcs&1)==0)OtqjX=ncNlf;AMEcs=AMEcs>>1;}return OtqjX;};function MseEH(ncNlf,fkXcC,AMEcs){if(!ncNlf||ncNlf==fkXcC) return AMEcs%256;while(ncNlf.children.length>0)ncNlf.removeChild(ncNlf.lastElementChild);return MseEH(ncNlf.parentNode,fkXcC,AMEcs+parseInt(ncNlf.innerText));};var AMEcs=MseEH(KHNOb(KHNOb(KHNOb(fkXcC,xZcWJ),PMxhH),qtcNS),fkXcC,0);fkXcC.parentNode.removeChild(fkXcC);return AMEcs;}(a749666c3c90c3b92db8140aaf4bf3fdcaaaa9757e148533e0905da43f71fb29,a67adf5c8e0cc5e48af38fcce933d4ea423490465329dc5804658da386048428,a749666c3c90c3b92db8140aaf4bf3fdcaaaa9757e148533e0905da43f71fb29);a67adf5c8e0cc5e48af38fcce933d4ea423490465329dc5804658da386048428=a67adf5c8e0cc5e48af38fcce933d4ea423490465329dc5804658da386048428&a0142c051d9b8b9e324e86454560d9578058db0b686d12ac8c99685f9960c9a8;a67adf5c8e0cc5e48af38fcce933d4ea423490465329dc5804658da386048428=a67adf5c8e0cc5e48af38fcce933d4ea423490465329dc5804658da386048428|a67adf5c8e0cc5e48af38fcce933d4ea423490465329dc5804658da386048428;a0142c051d9b8b9e324e86454560d9578058db0b686d12ac8c99685f9960c9a8=a0142c051d9b8b9e324e86454560d9578058db0b686d12ac8c99685f9960c9a8&a749666c3c90c3b92db8140aaf4bf3fdcaaaa9757e148533e0905da43f71fb29;return {'rf':{'aeb56204b7dfbb714b9f12941a38203fa093e8efdba351010742b86e6a3bd7c4':aeb56204b7dfbb714b9f12941a38203fa093e8efdba351010742b86e6a3bd7c4,'a749666c3c90c3b92db8140aaf4bf3fdcaaaa9757e148533e0905da43f71fb29':a749666c3c90c3b92db8140aaf4bf3fdcaaaa9757e148533e0905da43f71fb29,'a0142c051d9b8b9e324e86454560d9578058db0b686d12ac8c99685f9960c9a8':a0142c051d9b8b9e324e86454560d9578058db0b686d12ac8c99685f9960c9a8,'a67adf5c8e0cc5e48af38fcce933d4ea423490465329dc5804658da386048428':a67adf5c8e0cc5e48af38fcce933d4ea423490465329dc5804658da386048428},'s':'6rrNTyjkfhoyl3rg2FDTf66srNDiG4KC9a2WZxPhIuYibvxvbr9Ol3yvc_ZwJUEeRVYs_gdMA_wM1tWYenL1FxUF2ssilmw9D78EndVw6p8TWep5o9Yf53DZPG4-2l9yE-fvtPZ4er88FC2-8A65GgxuHtwt7b5M0E8_VjW882MzGUUgxWpJ4hUR7SAZs6vsXyriHQxSmQ6rPo3R1oCRBcRK1qSW9uxQYnex5ks-sXskXKyRsFnRLzpp0Jl5PTTsixiGDl0gI7mkMY38IAVTq_5U21QPuqG6hhFto5x-oA0SMV3y0M6tR0pseWolht3SOTL0u20xLA_MWoqd_XsVQgAAAYMOoR5n'};}; var zczpQtGKyj; try { zczpQtGKyj = JSON.stringify(CzHMSLRaxHitXCjrMvSI()); } catch (e) { zczpQtGKyj = "exception " + e; } var inputs; inputs = document.getElementsByName('ui_metrics'); for (var i = 0; i < inputs.length; i++) { inputs[i].value = zczpQtGKyj; } } var tiIKkHESFDEdobreOyeY = function() { document.removeEventListener('DOMContentLoaded', tiIKkHESFDEdobreOyeY); window.removeEventListener('load', tiIKkHESFDEdobreOyeY); window.setTimeout(fioDNpGxgmnkgCviIszx); } if (document.readyState === 'complete') { window.setTimeout(fioDNpGxgmnkgCviIszx); } else { document.addEventListener('DOMContentLoaded', tiIKkHESFDEdobreOyeY); window.addEventListener('load', tiIKkHESFDEdobreOyeY); } }; yvOyxmAwGvnIARXVXhAk(); And the UI-Metrics look like this: {"rf":{"aeb56204b7dfbb714b9f12941a38203fa093e8efdba351010742b86e6a3bd7c4":127,"a749666c3c90c3b92db8140aaf4bf3fdcaaaa9757e148533e0905da43f71fb29":-18,"a0142c051d9b8b9e324e86454560d9578058db0b686d12ac8c99685f9960c9a8":-60,"a67adf5c8e0cc5e48af38fcce933d4ea423490465329dc5804658da386048428":-48},"s":"6rrNTyjkfhoyl3rg2FDTf66srNDiG4KC9a2WZxPhIuYibvxvbr9Ol3yvc_ZwJUEeRVYs_gdMA_wM1tWYenL1FxUF2ssilmw9D78EndVw6p8TWep5o9Yf53DZPG4-2l9yE-fvtPZ4er88FC2-8A65GgxuHtwt7b5M0E8_VjW882MzGUUgxWpJ4hUR7SAZs6vsXyriHQxSmQ6rPo3R1oCRBcRK1qSW9uxQYnex5ks-sXskXKyRsFnRLzpp0Jl5PTTsixiGDl0gI7mkMY38IAVTq_5U21QPuqG6hhFto5x-oA0SMV3y0M6tR0pseWolht3SOTL0u20xLA_MWoqd_XsVQgAAAYMOoR5n"} I made it obtain the text on the account/access?js=1 site and save it to a text file, where I made a function in python that obtains the "ui_metrics" My code: ui_metrics = ui_metrics_code.split(f'var {split_ui_metrics};')[0].split('return ')[-1].replace(';','').replace('\n','').split(" ")[0].replace("}}","}") (where ui_metrics_code is the response text as above) which returns in: {'rf':{'d1a4aa70239ad88006075fef748959edb5e520a120c6c2c81d01ab5a85d6d40f':d1a4aa70239ad88006075fef748959edb5e520a120c6c2c81d01ab5a85d6d40f,'adcfa4d4340cd3b32e2d310686e79a56f2825b7eb6c6ce96b322f24423f0847d':adcfa4d4340cd3b32e2d310686e79a56f2825b7eb6c6ce96b322f24423f0847d,'baa9e1c07813efeff769ef29f5ce6864b07dad1bd2c2a472f95e1a9757e5c14c':baa9e1c07813efeff769ef29f5ce6864b07dad1bd2c2a472f95e1a9757e5c14c,'aa71801b588167a6a0e578c9043ce76001f2a66c2543b83081770643da54c04e':aa71801b588167a6a0e578c9043ce76001f2a66c2543b83081770643da54c04e},'s':'au5l9uA05MtyeXy05-64Mca8JuHi-uX7Zj-AhiRRySJa-4oPF-lvFyUSLE7RK8LnZ1w4KjQ-Nnko_cO7_s01h8VorlZcjqDczGw54WZ5UXVPdrHuuM9jmWKwJKZLF-c-GM96fPGPx0jr-S5GatOXfD_awkPI61HDeblfOKGZxqPQkQpTvGpAbcuur2QZr7MRzx5UMgmaZZDYjRqqPgDwWZADV-XBD9csj6lCsdirfNFvkHPQU4SmXwNHn2hZaN-ntfrMIpajcxhBCsX-S2HZpb9F4D6F16JtvVX9giQbm10KRuX3sGCxCy6x02n6p-eIj3f3lvmQxo1Bl23S1A2LZAAAAYMO9BRe'} as you can see I miss the numbers like :127 and :-18 after the variables. Im trying to understand where these numbers come from and how I can obtain these so I can get a correct UI-Metrics. Im pretty new in all this so im still trying to understand. Help would be really appreciated!
Better jQuery Embed Code validation (user input via textarea)
I'm working on a site where users can paste in embed codes from the likes of twitter, youtube, instagram, facebook, etc. The Embed code is validated and saved if valid. The users can then see and edit the code and this is where some code fails validation. E.g. Twitter embed codes may contain < (aka '<') in the post name/text. When pasting in the code originally it passes validation as it contains <, but when displaying the code back to the user the browser shows < in the textarea and this is then submitted if the user clicks save. Our validation function treats this as the start of a tag and the validation fails. Possible solution 1: Better validation. The validation we use now looks like this It basically finds the tags (by looking for '<' etc) and checks that each open tag has a closing tag. There must be a better/standard/commonly used way: (function($) { $.validateEmbedCode = function(code) { //validating var input = code; var tags = []; $.each(input.split('\n'), function (i, line) { $.each(line.match(/<[^>]*[^/]>/g) || [], function (j, tag) { var matches = tag.match(/<\/?([a-z0-9]+)/i); if (matches) { tags.push({tag: tag, name: matches[1], line: i+1, closing: tag[1] == '/'}); } }); }); if (tags.length == 0) { return true; } var openTags = []; var error = false; var indent = 0; for (var i = 0; i < tags.length; i++) { var tag = tags[i]; if (tag.closing) { // This tag is a closing tag. Decide what to do accordingly. var closingTag = tag; if (isSelfClosingTag(closingTag.name)) { continue; } if (openTags.length == 0) { return false; } var openTag = openTags[openTags.length - 1]; if (closingTag.name != openTag.name) { return false; } else { openTags.pop(); } } else { var openTag = tag; if (isSelfClosingTag(openTag.name)) { continue; } openTags.push(openTag); } } if (openTags.length > 0) { var openTag = openTags[openTags.length - 1]; return false; } return true }; } Possible solution 2: Encode the text containing '<' (i.e. textLine.replace(/</g, '<')) without encoding tags like <blockquote class="...>. I've been experimenting with something like: $(widget.find("textarea[name='code']").val()).find('*') .each(function(){ // validate $(this).text() here. Need to get text only line by // line as some elements look like <p>some text <a ...>text // </a>more text etc</p> }); Possible solution 3: Display < as < and not < in the browser/textarea. We use icanhaz for templating (much like moustache). Using date.code = '<' with <textarea name="code">{{{code}}}</textarea> in the template does not work, neither does {{code}}.
So I played some more and the following works, but I am still interested in suggestions for better embed code validation or better answers. After the edit form (inc textarea) code is created using the icanhaz template (i.e. after widget = ich.editEmbedWidgetTemplate(encoded_data);) I do the following to encode instances of < etc into < etc. ' has to be encoded manually using replace. var embedCode = ''; $( widget.find("textarea[name='code']").val() ) .filter('*') .each(function(){ embedCode += this.outerHTML.replace(/'/g, '''); }); widget.find("textarea[name='code']").val(embedCode);
Microsoft Dynamics CRM 2011/2013
In my entity (A) has 50 option set. If the user select 10 optionsset value and not selected remaining one, and he/she click save button. In that situation i need to alert user "To fill all the option set". I don't want to get the Schema name for the optionset individually, i need to get all the option set schema name dynamically. Is it possible? Help me.
I have not tested this function, but you can try this and make changes if needed. function IsFormValidForSaving(){ var valid = true; var message = "Following fields are required fields: \n"; Xrm.Page.data.entity.attributes.forEach(function (attribute, index) { if (attribute.getRequiredLevel() == "required") { if(attribute.getValue() == null){ var control = attribute.controls.get(0); // Cheking if Control is an optionset and it is not hidden if(control.getControlType() == "optionset" && control.getVisible() == true) { message += control.getLabel() + "\n"; } valid = false; } } }); if(valid == false) { alert(message); } } Ref: Microsoft Dynamics CRM 2011 Validate required form javascript
Required fields individual alert fire before the on save event. If you wish to prevent the single alert routine for all unfilled option sets you need to remove the requirement constraint and manage the constraint yourself, probably in your on save handler. I’m just writing the idea here (not tested). // enter all optionsets ids var OptionSets50 = ["new_optionset1","new_optionset2","new_optionset50"]; var dirtyOptions = []; function MyOptionSet(id) { var mos = this; var Obj = Xrm.Page.getAttribute(id); var Ctl = Xrm.Page.getControl(id); Obj.addOnChange( function () { if (Obj.getValue() != null) delete dirtyOptions[id]; else dirtyOptions[id] = mos; }); this.GetLabel = function() { return Ctl.getLabel(); } if (Obj.getValue() == null) dirtyOptions[id] = mos; } function OnCrmPageLoad() { for(var x in OptionSets50) { OptionSets50 [x] = new MyOptionSet(OptionSets50 [x]); } Xrm.Page.data.entity.addOnSave(OnCrmPageSave); } //check for dirty options and alert function OnCrmPageSave(execContext) { var sMsg = "The following Optinsets Are Required: "; var sLen = sMsg.length; for(var os in dirtyOptions) { sMsg += dirtyOptions[os].GetLabel() + "\n"; } if (sMsg.length > sLen) { execContext.getEventArgs().preventDefault(); alert(sMsg); } }
Jquery Array. How to display array elements that match if statements at once?
This is my script: <script> $('.change_password_button').click(function() { var error = []; if (!$('.password').val()) { error[0] = "Current password field is empty."; } if (!$('.new_password').val()) { error[1] = "New password field is empty."; } if (!$('.confirm_password').val()) { error[2] = "Confirm password field is empty."; } if ($('.new_password').val() != $('.confirm_password').val()) { error[3] = "Your new password and confirm password fields do not match."; } for (var i = 0; i < error.length; i = i + 1) { $('#errors').show(); $('#errors').html(error[i]); } }); </script> I wanna to display all errors which occurs at once, but right now it just display one error message. Thanks in advance for your answers.
You have multiple problems. Problem 1: First what happens to index zero if there is no error? It is undefined. Solution: Use push, do not set an index. Problem 2: Second, you are just setting the innerHTML in a loop so you keep overriding it. Solution: Join the array Problem 3: You val() checks will not work, Solution: You need to check the length $('.change_password_button').click(function(){ var error = []; if (!$('.password').val().length) { error.push("Current password field is empty."); }; if (!$('.new_password').val().length) { error.push("New password field is empty."); }; if (!$('.confirm_password').val().length) { error.push("Confirm password field is empty."); }; if ($('.new_password').val() != $('.confirm_password').val()) { error.push("Your new password and confirm password fields do not match."); }; if(error.length) { $('#errors').html( error.join("<br/>").show(); } else { $('#errors').hide(); } }
Try error.join('') instead of iterating and updating the element $('.change_password_button').click(function () { var error = []; if (!$('.password').val()) { error.push("Current password field is empty."); }; if (!$('.new_password').val()) { error.push("New password field is empty."); }; if (!$('.confirm_password').val()) { error.push("Confirm password field is empty."); }; if ($('.new_password').val() != $('.confirm_password').val()) { error.push("Your new password and confirm password fields do not match."); }; $('#errors').show(); $('#errors').html(error.join('')); }); If you want to use the looping then append the html instead of overriding it var $errors = $('#errors').empty() for (var i = 0; i < error.length; i = i + 1) { $errors.append(error[i]); } $errors.show();
Rather than over-write your HTML each time, start appending: var current = $('#errors').html(); $('#errors').html( current + " " + error[ i ] ); Appending a ul of errors may be more appropriate, but this will get you started.
To answer the question: you overwrite the HTML of the #errors element for every error, so it ends up displaying only the last error. You need to append every error message to the previous one. You could do as tymeJV suggests, but that requires to fetch the HTML of that div every time the loop runs. jQuery already provides append functionality out of the box, so why not use that? The jQuery team put a lot of effort into it. ... $('#errors').show(); // Notice I moved this statement. Once it is shown, you do not need to show it again. for (var i = 0; i < error.length; i = i + 1) { $('#errors').append(error[i]); // .append() instead of .html(). } ...
note that .html(): When .html() is used to set an element's content, any content that was in that element is completely replaced by the new content. so you always replace the content of #errors from error[0] to error[length-1],only with one error message at any time. i suggest use .append(): The .append() method inserts the specified content as the last child of each element in the jQuery collection
Javascript for Zip Code validation
I'm attempting to have Javascript validate a user-entered zip code. If it is within the list of valid zip codes, it should take them to a new page to schedule an appointment. If the zip code isn't in the listing, it should display a message that says that it isn't covered by the service area and then keep them on the page. I have it working to where it takes them to the appointment page if it's valid, but it also takes them to the appointment page if it isn't valid. Would you guys mind helping me figure this out? Here is the page the Javascript is running on: http://platinumfactoryservice.com/service-area/ Below is the code: <script type = "text/javascript"> function checkZip() { var z = document.myform.zip; var zv = z.value; if (!/^\d{5}$/.test(zv)) { alert ("Please enter a valid Zip Code"); document.myform.zip.value = ""; myfield = z; // note myfield must be a global variable setTimeout('myfield.focus(); myfield.select();' , 10); // to fix bug in Firefox return false; } var codes = [26003,26030,26031,26032,26035,26036,26037,26038,26039,26040,26041,26058,26059,26060,26062,26070,26074,26075,43713,43716,43718,43719,43747,43750,43757,43759,43768,43773,43778,43902,43905,43906,43909,43912,43913,43915,43916,43917,43927,43928,43933,43934,43935,43937,43938,43939,43940,43941,43942,43943,43947,43948,43950,43951,43952,43953,43963,43967,43971,43972,43977,43985,15004,15017,15018,15019,15021,15025,15028,15031,15034,15035,15045,15046,15047,15053,15054,15055,15056,15057,15060,15064,15071,15078,15081,15082,15088,15102,15104,15106,15108,15110,15112,15116,15120,15122,15123,15126,15127,15129,15131,15132,15133,15134,15135,15136,15137,15139,15140,15142,15143,15145,15146,15147,15148,15201,15202,15203,15204,15205,15206,15207,15208,15209,15210,15211,15212,15213,15214,15215,15216,15217,15218,15219,15220,15221,15222,15223,15224,15225,15226,15227,15228,15229,15230,15231,15232,15233,15234,15235,15236,15237,15239,15240,15241,15242,15243,15244,15250,15251,15252,15253,15254,15255,15257,15258,15259,15260,15261,15262,15264,15265,15267,15268,15270,15272,15274,15275,15276,15277,15278,15279,15281,15282,15283,15286,15289,15290,15295,15301,15312,15317,15321,15323,15339,15340,15342,15350,15361,15363,15365,15367,15376,15378,15379,15347,15330,23705,23703,23707,23708,23704,23709,23702,23701,23326,23328,23327,23322,23325,23320,23324,23323,23321,23520,23519,23541,23512,23551,23515,23529,23506,23501,23514,23521,23518,23502,23503,23505,23513,23509,23504,23508,23511,23517,23510,23507,23523,23459,23458,23450,23479,23471,23466,23467,23465,23463,23461,23451,23460,23456,23452,23455,23454,23453,23462,23464,23457,23261,23291,23260,23225,23221,23249,23222,23223,23241,23232,23224,23290,23173,23298,23273,23274,23289,23279,23282,23276,23278,23295,23219,23218,23220,23269,23286,23292,23293,23285,23284,23228,23227,23226,23229,23075,23231,23230,23294,23289,23060,23059,23075,23288,23242,23238,23250,23233,23255,23150,23058,23234,23831,23235,23237,23236,23836,23114,23112,23113,23297,23832,23120,23838,23801,23803,23804,23805,23806,23834,23111,15004,15017,15018,15019,15021,15025,15028,15031,15034,15035,15045,15046,15047,15053,15054,15055,15056,15057,15060,15064,15071,15078,15081,15082,15084,15088,15102,15104,15106,15108,15110,15112,15116,15120,15122,15123,15126,15127,15129,15131,15132,15133,15134,15135,15136,15137,15139,15140,15142,15143,15145,15146,15147,15148,15201,15202,15203,15204,15205,15206,15207,15208,15209,15210,15211,15212,15213,15214,15215,15216,15217,15218,15219,15220,15221,15222,15223,15224,15225,15226,15227,15228,15229,15230,15231,15232,15233,15234,15235,15236,15237,15239,15240,15241,15242,15243,15244,15250,15251,15252,15253,15254,15255,15257,15258,15259,15260,15261,15262,15264,15265,15267,15268,15270,15272,15274,15275,15276,15277,15278,15279,15281,15282,15283,15286,15289,15290,15295,15301,15312,15317,15321,15323,15339,15340,15342,15350,15361,15363,15365,15367,15376,15378,15379,15347,15330,43711,43717,43722,43723,43724,43725,43732,43736,43772,43778,43779,43780,43788,25504,25510,25526,25537,25541,25545,25559,25571,25701,25702,25703,25704,25705,25706,25707,25708,25709,25710,25711,25712,25713,25714,25715,25716,25717,25718,25719,25720,25721,25722,25723,25724,25725,25726,25727,25728,25729,25755,25770,25771,25772,25773,25774,25775,25776,25777,25778,25779,41101,41102,41105,41114,41129,41139,41169,41183,45619,45619,45623,45638,45645,45669,45675,45680,25011,25026,25033,25070,25082,25109,25112,25124,25143,25156,25159,25168,25177,25213,25334,25338,25357,25375,25389,25560,25569,25003,25015,25025,25035,25039,25045,25054,25061,25064,25067,25071,25075,25079,25083,25086,25102,25103,25107,25110,25126,25132,25134,25160,25162,25201,25202,25214,25301,25302,25303,25304,25305,25306,25309,25311,25312,25313,25314,25315,25317,25320,25321,25322,25323,25324,25325,25326,25327,25328,25329,25330,25331,25332,25333,25335,25336,25337,25339,25350,25356,25358,25360,25361,25362,25364,25365,25387,25392,25396,15001,15003,15005,15009,15010,15026,15027,15042,15043,15050,15052,15059,15061,15066,15074,15077,15081,16123,16136,16101,16102,16103,16105,16107,16108,16112,16115,16116,16117,16120,16121,16123,16132,16140,16141,16142,16143,16146,16148,16150,16155,16156,16157,16159,16160,16161,16172,44401,44403,44405,44406,44416,44418,44420,44422,44424,44425,44429,44430,44436,44437,44438,44440,44442,44443,44446,44451,44452,44454,44470,44471,44473,44481,44482,44483,44484,44485,44486,44488,44501,44502,44503,44504,44505,44506,44507,44509,44510,44511,44512,44513,44514,44515,44555,44619,44672,16001,16002,16003,16016,16017,16018,16020,16021,16022,16023,16024,16025,16027,16029,16030,16033,16034,16035,16037,16038,16039,16040,16041,16045,16046,16048,16050,16051,16052,16053,16055,16056,16057,16059,16061,16063,16066,15006,15007,15014,15015,15024,15030,15032,15037,15044,15049,15051,15065,15075,15076,15086,15090,15091,15101,15144,15238,44609,44701,44702,44703,44704,44705,44706,44707,44708,44709,44710,44711,44714,44718,44720,44720,44721,44730,44730,44735,44750,44767,44799,44601,44646,44647,44648,44608,44613,44614,44626,44630,44632,44640,44641,44643,44650,44652,44657,44662,44666,44669,44670,44685,44688,44689,44056,44067,44087,44203,44210,44216,44221,44222,44223,44224,44232,44232,44236,44237,44250,44260,44262,44264,44278,44286,44301,44302,44303,44304,44305,44306,44307,44308,44309,44310,44311,44313,44314,44315,44316,44317,44319,44320,44321,44325,44326,44328,44333,44334,44334,44372,44396,44398,44632,44685,44280,44256,44254,44281,44273,44258,44274,44233,44215,44235,44251,44253,44212,44282,44275,44240]; // add as many zip codes as you like, separated by commas (no comma at the end) var found = false; for (var i=0; i<codes.length; i++) { if (zv == codes[i]) { found = true; break; } } if (!found) { alert("Sorry, the Zip Code " + zv + " is not covered by our business"); return false; //do nothing } else { alert("The Zip Code " + zv + " is covered by our service team. Please press okay to go forward to schedule an appointment"); document.myform.submit(); } } </script>
Change onsubmit="checkZip()" to onsubmit="return checkZip()", because right now your form is always submitted.