osmvalidation v0.0.3
osmValidation
JavaScript library to validate various value-types of OpenStreetMap.
Useable in PlainJS, nodeJS and jQuery.
Installation
node.js
Install osmvalidation via npm:
npm install osmvalidationvar osmValidation = require('osmvalidation');
if(osmValidation.phone('+49 123 456-789')) {
// do stuff
console.log(osmValidation.msg);
}jQuery
Download osmValidation.js and include it before you use it.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>osmValidation</title>
</head>
<body>
<form>
<input type="url" />
<input type="submit" />
</form>
<script type="text/javascript" src="js/jQuery.js"></script>
<script type="text/javascript" src="js/osmValidation.js"></script>
<script type="text/javascript">
$('input[type=submit]').attr('disabled','disabled');
$('input[type=url]').onkeyup(function(){
if($(this).osmValidate()) {
$('input[type="submit"]').removeAttr('disabled');
} else {
$('input[type=submit]').attr('disabled','disabled');
}
});
</script>
</body>
</html>plain js
Download osmValidation.js and include it before you use it.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>osmValidation</title>
</head>
<body>
<form>
<input id='url' type="url" />
<input id='submit' type="submit" />
</form>
<script type="text/javascript" src="js/jQuery.js"></script>
<script type="text/javascript" src="js/osmValidation.js"></script>
<script type="text/javascript">
document.getElementById('submit').onclick(function(){
if(osmValidation.url(document.getElementById('url').value) {
alert("valid");
}
});
</script>
</body>
</html>Functions
phone(string)Validate phonenumbersmail(string)Validate mailaddressesurl(string)Validate urlsfacebook(string)Validate facebook IDgoogle(string)Validate google plus IDtwitter(string)Validate twitter IDwikipedia(string)Validate wikipedia-Page tagwikidata(string)Validate wikidata-ID
You can validate a givven String as parameter. The return is a boolean (false or true). If you want more informations, you can read msg. This function returns a string with more informations about the validation. If you want to use your own error-messages: every msg-string is a public string-constant of osmValidation. You can compare the return with the constant.
Available constants:
PLAIN_FLAGempty string, default messagePHONE_EMERGENCYphonenumber is a valid emergency numberPHONE_VALIDphonenumber is a valid international numberPHONE_INVALIDnumber is not a emergency number or an international phonenumber (+\d{1,4} \d+( \d+(-\d+)))MAIL_VALIDemail is validMAIL_INVALIDemail is invalidURL_PROTOCOLL_INVALIDURL has no or wrong protocoll. At this time, I allow only http or httpsURL_LOCAL_ADDRESSURL to a local service is not usefulURL_HOST_INVALIDHost is not a ipv4- or ipv6-address and it has no fqdnURL_INVALIDURL seems brokenURL_VALIDURL is validFACEBOOK_ID_ONLYcorrect facebook IDFACEBOOK_URL_VALIDcorrect facebook-page URLFACEBOOK_URL_INVALIDNeither a valid facebook ID nor a plain link (without parameter) to a pageTWITTER_ID_ONLYcorrect twitter IDTWITTER_URL_VALIDcorrect twitter-page URLTWITTER_URL_INVALIDNeither a valid twitter ID nor a plain link (without parameter) to a pageGOOGLE_ID_ONLYcorrect google IDGOOGLE_NAME_ONLYcorrect google plus nameGOOGLE_URL_VALIDcorrect google-page URLGOOGLE_URL_INVALIDNeither a valid google ID, name nor a plain link (without parameter) to a pageWIKIPEDIA_INCLUDING_LANGcorrect wikipedia page-name including language-tagWIKIPEDIA_EXCLUDING_LANGcorrect wikipedia page-name without language-tagWIKIPEDIA_URLcorrect wikipedia urlWIKIPEDIA_INVALIDWikipedia-Tag not validWIKIDATA_VALID_TAGcorrect wikidata tagWIKIDATA_INVALIDWikidata-Tag not valid
jQuery
There is a special jQuery-Function. You can validate an object by the function osmValidate(). It use the type- or data-type-attribute of the object as identifier for the validation-function. It returns a boolean, NOT the object itself!