tools.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. export default {
  2. // Parse <value> in: http://server/?variable=value
  3. getQueryVariable: function (variable) {
  4. var query = location.search.substring(1);
  5. var vars = query.split('&');
  6. for (var i = 0; i < vars.length; i++) {
  7. var pair = vars[i].split('=');
  8. if (decodeURIComponent(pair[0]) == variable) {
  9. return decodeURIComponent(pair[1]);
  10. }
  11. }
  12. throw variable + ': not found';
  13. },
  14. // Trim string
  15. trim: function(str) {
  16. // Make sure we trim BOM and NBSP
  17. var rtrim = /^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g;
  18. return str.replace(rtrim, '');
  19. },
  20. uniqueid: function(){
  21. // always start with a letter (for DOM friendlyness)
  22. var idstr=String.fromCharCode(Math.floor((Math.random()*25)+65));
  23. do {
  24. // between numbers and characters (48 is 0 and 90 is Z (42-48 = 90)
  25. var ascicode=Math.floor((Math.random()*42)+48);
  26. if (ascicode<58 || ascicode>64){
  27. // exclude all chars between : (58) and @ (64)
  28. idstr+=String.fromCharCode(ascicode);
  29. }
  30. } while (idstr.length<32);
  31. return (idstr);
  32. },
  33. validateEmail: function(email) {
  34. const re = /^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
  35. return re.test(String(email).toLowerCase());
  36. }
  37. }