1

I am new to Javascript. So I had a doubt for the following function, how to display the error message next to text box? right now it is showing a alert message, but I need to change the alert message and need to display it next to textbox?

function AllowLock(){
    if (!locker.lock.value.match(/[a-zA-Z]$/) && locker.lock.value !="") {
        locker.lock.value="";
        alert("Please Enter only valid lock");
    }
    if(locker.lock.value.length > 5)
        alert("max length exceeded");
    } 
}
1
  • 2
    U should create a custom alert that u then can position with CSS next to your textbox. An approach is have a hidden div in your HTML and make it visible when the errors occurs Commented Sep 23, 2013 at 8:05

3 Answers 3

1

you must have any container for that message, so you can create element and then append your message to that, something like this, with jquery

 <input type="text" id="kuku"></input>

$('#kuku').after('<div></div>').html("your message");
Sign up to request clarification or add additional context in comments.

5 Comments

can u able to do it with table?
what do you mean with table ?
u mean append table instead of div ? if yes, actually u can append any element with after function. u mean this $('#kuku').after('<table><tr><td></td></tr></table>').html("your message"); ?
@ashokkumar can put more clear html code please, i cant understand what u did, in jsFiddle, put your html code in html part, css in css part, and don't forget to format it
yeah i got footer at the bottom,but how to align that header to the top using table tag?
0

You can create a span element next to your textbox

<input type="text" /><span id="msg"></span>

and put your message.

document.getElementById("msg").innerHTML = "your message";

Comments

0

Expanding on anna's answer here is a example usage.. this code is the called on the on click of my 'continue' button. Removes all existing error messages at start so they dont stack up etc.

<input id="userZip" type="number">
<input id="userName" type="text">
$('.errorText').remove();
userZip.style.border = "1px solid black";
userName.style.border = "1px solid black";
if(userName.value.length >= 3){
  if(userZip.value.length == 5){
    //success
    loginPanel.style.display = "none";
    location.href='#appNavigation/'+'mapNav';
  } else {
    userZip.style.border = "1px solid red";
    $('#userZip').after('<div class="errorText">Zipcode required. 5 numbers</div>');
  }
} else {
  userName.style.border = "1px solid red";
  $('#userName').after('<div class="errorText">Username required. 3 or more characters</div>');
}

1 Comment

Welcome to Stackoverflow. Please read how to write a good answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.