code_junkie

javascript function which ensures a text contains only dollar symbols formatted as $placeholder$

Nov 14th, 2011
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.77 KB | None | 0 0
  1. function validateFormat(text) {
  2. // Do stuff
  3. }
  4.  
  5. validateFormat("blab abalaba $something$ avava $affo$") -> true
  6. validateFormat("blab abalaba $something$ avava $ affo$") -> false because of the white space
  7. validateFormat("blab abalaba $1so3mething$ avava $affo$") -> false because of the numbers
  8. validateFormat("blab abalaba $something") -> false because of unclosed placeholder
  9.  
  10. // delete all valid placeholders
  11. text = text.replace(/$[a-z]+$/gi, "");
  12.  
  13. // are there any "$" left?
  14. if (text.search(/$/) != -1) {
  15. return false;
  16. }
  17. return true;
  18.  
  19. matches = text.match(/$[a-z]+$|$/gi);
  20. if (matches) {
  21. for (var i = 0; i < matches.length; i++) {
  22. if (matches[i] == '$') { return false };
  23. }
  24. }
  25. return true;
  26.  
  27. text.match(/$/g).length/2 == text.match(/$[a-z]+$/g).length
Add Comment
Please, Sign In to add comment