Skip to content

Instantly share code, notes, and snippets.

@englishextra
Last active October 17, 2016 21:42
Show Gist options
  • Save englishextra/b5aaef8b555a3ba84c68a6e251db149d to your computer and use it in GitHub Desktop.
Save englishextra/b5aaef8b555a3ba84c68a6e251db149d to your computer and use it in GitHub Desktop.
Check if string represents a valid HTML id
/*!
* Check if string represents a valid HTML id
* gist.github.com/englishextra/b5aaef8b555a3ba84c68a6e251db149d
* jsfiddle.net/englishextra/z19tznau/
* @param {String} a text string
* @param {Int} [full] if true, returns with leading hash/number sign
* isValidId(a,full)
*/
var isValidId = function(a, full) {
return full ? /^\#[A-Za-z][-A-Za-z0-9_:.]*$/.test(a) ? !0 : !1 : /^[A-Za-z][-A-Za-z0-9_:.]*$/.test(a) ? !0 : !1;
};
alert(isValidId("#dffdfddffd",!0));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment