Skip to content

Instantly share code, notes, and snippets.

@vinodchauhan7
Created December 26, 2019 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vinodchauhan7/497643664382ace761034ee6ae080afe to your computer and use it in GitHub Desktop.
Save vinodchauhan7/497643664382ace761034ee6ae080afe to your computer and use it in GitHub Desktop.
const getName = document.getElementById("name");
const getUserId = document.getElementById("userId");
const registerButton = document.getElementById("register");
const getResult = document.getElementById("result");
const errorBox = document.getElementById("error");
const checkLettersOnly = input => {
if (/^[a-zA-Z]+$/.test(input)) {
return true;
}
return false;
};
const parseInput = input => {
return parseInt(input);
};
const checkUserIdValidation = input => {
console.log(input);
console.log(typeof parseInt(input) === "number" && !isNaN(input));
return typeof parseInt(input) === "number" && !isNaN(input);
};
const hideErrors = () => {
errorBox.classList.add("invisible");
};
hideErrors();
const handleError = (name, userId) => {
var message = "Please add valid value for => \n ";
if (!checkLettersOnly(name)) {
message += `Name : ${name} must contains only characters \n`;
}
if (!checkUserIdValidation(getUserId.value)) {
message += `UserId : ${userId} must contains only number`;
}
errorBox.classList.remove("invisible");
errorBox.innerText = message;
};
registerButton.addEventListener("click", () => {
var name = getName.value;
var userId = getUserId.value;
hideErrors();
getResult.innerText = "";
if (checkLettersOnly(name) && checkUserIdValidation(userId)) {
getResult.innerText = `User ${name} has userId : ${userId}`;
} else {
handleError(name, userId);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment