if you write javascript code in master page.it will refer in all the pages that inherit from the masterpage.
instead of that you what you can do is set some page as (which inherited from master page) as start up page(by write click that page in solution explorer).
then write the login function and validation inside that page. You can write javascript code inside other pages which inherit from master page.
write javascript code out side the all html elements.
ex:
<script type="text/javascript">
function myfunction(txt) {
alert(txt);
var x = document.getElementById(txt);
for (i = 0; i < x.Items.length; i++) {
var tag = Items[i];
alert(tag.getAttribute("id"));
}
}
</script>
or the else you can use page that does not inherit from master page as start up page and use all login validation inside that page. then if the valid login, transfer to the page which inherit from master page.