Custom Error handling in js [closed] - javascript

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I have a form that takes students' information. On this form, there is an age entry with legal ages 8 to 20. If the age is less than 8 or greater than 20, a js alert pops up informing the student of the illegal age.
I am seeking for a way to create custom error handlers rather than just using the unfriendly js.alert() method. Any pointers to how this could be done will be appreciated.

To make error messages more user friendly here's one solution.
Create a div with no content. If an error is to be displayed, it will be displayed inside this div.
Here's how to do it with jQuery.
$('button').on('click', function() {
var value = $('input').val();
$('#error-message').html((value < 8 || value > 20) ? 'Error' : '');
});
#error-message {
color: red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type='number' />
<button>Submit</button>
<div id='error-message'></div>
Here's a JavaScript solution
document.getElementById('submit').addEventListener('click', function() {
var age = document.querySelector('input').value;
var error = document.getElementById('error-message');
error.innerHTML = (age < 8 || age > 20) ? 'Error' : '';
});
#error-message {
color: red;
}
<input type='number' />
<button id='submit'>Submit</button>
<div id='error-message'></div>

Related

javascript code to display an alert message after 15 keystrokes [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
In my project, there is a text box to enter the password and the password characters limit is up to 15. So, I want to display an alert message through javascript as soon as the user tries to enter the 16th character. Please help!
Try this.
function alrtMsg() {
var x = document.getElementById("pwd").value;
if(x.length > 16){
alert("maximum length is 16");
document.getElementById("pwd").value = '';
}
}
<html>
<body>
<input type="password" id="pwd" onkeyup="alrtMsg()">
</body>
</html>
Try this on Javascript:
document.getElementById("password").onkeyup = function() {
var text = document.getElementById("password").value
if(text.length> 15){
alert("too much text")
}
};
<input id="password" type="password" placeholder="password">
you need to mention your code what you tied. Anyway if you don't know try this code
<input type="text" onkeypress="myFunction(this)" maxlength="5">
<script>
function myFunction(e) {
var maxlen= e.maxLength;
if(e.value.length >= maxlen)
alert("Max length is limited to" +maxlen );
}
</script>
Using JQuery:
$("#inputFieldId").on("input propertychange", function(){
var val = this.value;
if(val.length > 15) {
this.value = val.substring(0,15); //comment if you don't want to remove the 16th character
alert("Maximum 15 characters allowed!");
}
});

How to validate given textbox? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
This is textarea box and I want to validate it. I want to use required field validation. How can I do that?. I have tried validating textarea box using name and CSS class but I have failed to do so.
<textarea <%#!((GPNS.BusinessLayer.SpecialItems.SpecialItem)Container.DataItem).Code.Equals("OTH", StringComparison.InvariantCultureIgnoreCase) ? "style='display: none;'" : string.Empty%> id="text<%#((GPNS.BusinessLayer.SpecialItems.SpecialItem)Container.DataItem).ID%>" maxlength="50" placeholder="Enter other item details"></textarea>
You can use below from http://www.w3schools.com/js/js_validation.asp
function validateForm() {
var x = document.forms["myForm"]["fname"].value;
if (x == null || x == "") {
alert("Name must be filled out");
return false;
}
}
You can simply use required=true property
<textarea required=true maxlength="50" placeholder="Enter other item details" <%#!((GPNS.BusinessLayer.SpecialItems.SpecialItem)Container.DataItem).Code.Equals("OTH", StringComparison.InvariantCultureIgnoreCase) ? "style='display: none;'" : string.Empty%> id="text<%#((GPNS.BusinessLayer.SpecialItems.SpecialItem)Container.DataItem).ID%>" ></textarea>
OR
<script type="text/javascript">
$(document).ready(function () {
$("form").validate({
rules:{
textarea_name:{
required:true,
maxlength:50
}
}
});
});
</script>

Subtract 1 from total if check box is checked [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
Say you have a checkbox and beside it you show any numbers, be it 10 and when user clicks on the checkbox then that 10 becomes 9 but if unchecks then it again becomes 10.
<input type="checkbox" id="credits" name="credits" <?php echo $checked;?> /> (10) Credit
The value of credit would be fetched from database.
$(function () {
$('#credits').change(function () {
var currentValue = parseInt($('#credit-amount').text());
var newValue = currentValue + ($(this).prop('checked') ? -1 : 1);
$('#credit-amount').text(newValue);
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="checkbox" id="credits" name="credits" /> (<span id="credit-amount">10</span>) Credit

Display price between given dates [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Here is the structure and data of my price
if number is between 0 and 2000 (or in other word the number is less then 2000) - then must appear tax "20"
if number is between 2001 and 5000 - then must appear tax 35
Price:<input type=text name=price><br>
Tax: <input type=text name=tax>
First you should add labels for your inputs, they are linked to the input with ids
<label for="priceInput">Price:</label><input id="priceInput" type=text name=price><br>
<label for="taxInput">Tax:</label><input id="taxInput" type=text name=tax>
And in your jquery:
//Function is executed when the priceInput is changed, so when you enter a value
$("#priceInput").change(function(){
//Test if the value of the input is < 2000 and >0
if (($( "#priceInput" ).val()<2000) && ($("#priceInput").val()>0)) {
//Set the value of the tax input to 20
$("#taxInput").val(20)
}
//test if tyhe value is >2000 and <5000
else if(($( "#priceInput" ).val()<5000) && ($("#priceInput").val()>2000)){
$("#taxInput").val(35)
}
else{
$("#taxInput").val("")
}
});
JSFiddle link
function setPrice()
{
if ($('[name=price]').val() < 2000)
{
$('[name=tax]').val(20);
}
else
{
$('[name=tax]').val(35);
}
}

Javascript is working fine but not in jQuery [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
This code works fine for me in Javascript.
function check_u() {
var errormessage = document.getElementById("errorname");
var user = document.forms["login"]["user"].value;
if (user == null || user == "") {
errormessage.innerHTML = "Please enter your user id";
} else {
errormessage.innerHTML = "";
}
}
function check_p() {
var errormessage = document.getElementById("errorpass");
var pass = document.forms["login"]["password"].value;
if (pass == null || pass == "") {
errorShow.innerHTML = "Password cannot be blank";
} else {
errorShow.innerHTML = "";
}
}
My html is :
<input type="text" name="user" autocomplete="off" onBlur="check_u()" />
<input type="text" name="password" autocomplete="off" onBlur="check_p()" />
<div id="errorname" />
I have written an alternative for this code in jQuery. In JavaScript it works fine but in jQuery when I don't enter any input first time it is showing error message. I enter some value to it then it clears the error message. Now if I am leaving the input blank the error message doen't show up. Here is my jQuery code:
function check_u(){
var fieldValue = $("input[name=user]").val();
if(fieldValue==""||fieldValue==null){
$("#errorname").html('<div id = "error_left"></div>'+
'<div id = "error_right"><p>This is a required field</p></div>');
}else{
$("#errorname").hide();
}
}
Why, if on repeated calls, does my errorname div not show up?
Does .hide() do more than just clear out the div?
In the javascript else clause, you are clearing the contents, but in the jQuery clause you are hiding the entire error element.
Instead of
$("#errorname").hide()
try
$("#errorname").html("")
You need to show errorname after setting the html
$("#errorname").html('<div id = "error_left"></div>'+
'<div id = "error_right"><p>This is a required field</p></div>').show();
You are hiding the errorname div in case of a valid entry, then if value becomes invalid then you need to set the error message and set the visibility to of the div.
demo: Plunker
Your onblur is calling check_u() but your other jQuery example's function name is checkUser()

Categories