0
<script>
function validate(){
u=document.CustomerLogin.UserName.value;
p=document.CustomerLogin.Password.value;
rp=document.CustomerLogin.RePassword.value;

if (u==""){
 document.write("Enter a unique username");
 return false;
 }
if (p!=rp){
 document.write("Retype Password Incorrect");
 return false;
 }
return true;
}
</script>

The messages are printed on separate page but i want them to be printed at the same place in front of text box! Please help. Thanks!

1
  • it's not 'writing to a separate page' but rather you over-write the entire contents of your DOM when you use the 'document.write' statement. Use innerHTML. Commented May 24, 2012 at 21:50

3 Answers 3

1

Never use Document write, it's hazardous...

document.getElementById("anidofanElementinthePage").innerHTML = " string to add by javascript";

<script>
function validate(){
u=document.CustomerLogin.UserName.value;
p=document.CustomerLogin.Password.value;
rp=document.CustomerLogin.RePassword.value;

if (u==""){
  document.getElementById("theIdOfyourTextBlocInForm").innerHTML = "Enter a unique username";
 return false;
 }
if (p!=rp){
 document.getElementById("theIdOfyourTextBlocInForm").innerHTML = "Retype Password Incorrect";
 return false;
 }
return true;
}
</script>
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! And how to print these messages in front of text boxes now ?
Please the flag the answer. just do like that : <p id="errorlogin"></p> <input type="text" /> and in javascript : document.getElementById("errorlogin").innerHTML = "Retype Password Incorrect";
0

document.write will simply append the message to the end of the page. The simplest solution would be change it to an alert like this:

if (u==""){
 alert("Enter a unique username");
 return false;
}

Otherwise you will need to create new DOM elements(either before hand or when an error is detected) to hold the message and position them next to the inputs

1 Comment

Actually after the page has finished loading document.write will overwrite the contents of the page.
0

you would need to detect text change. One way is to use this:

<input name="blah" onchange="validate()" ... />

EDIT To add text on the same page, include the script using <script src=...> and then add <div id='validation[i]'></div> next to each of the inputs where [i] is increased by 1 each time (so validation1 validation2 so on). Then, use getElementById('validation[i]').innerHtml to change the value.

1 Comment

No! I know that already. onchange will work even without SUBMIT click but it also redirects to another page to print error! I want the error on same page. I want this -->For example: Pass: 123 Retype Password: 321 Error:[Password mismatched]

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.