• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

How to reflect Javascript Validation errors just below the fields?

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a basic html form :

<form method="post" action=""
onsubmit="return validate(this);">

Username: <input type="text" name="Username" size="10"><br>
Password: <input type="password" name="Password" size="10"><br>

<input type="submit" value="Submit"> <input type="reset"
value="Reset Form">
</form>

<script>
function validate(form){
var userName = form.Username.value;
var password = form.Password.value;
var errors = [];

if (checkLength(userName)) {
document.getElementByName(userName).innerHTML= 'Message';
errors.push("You must enter a username.");
}

if (checkLength(password)) {
errors.push("You must enter a password.");
}
var msg = "There were some problems...\n";
var numError;
if (errors.length > 0) {
//reportErrors(errors);
for (var i = 0; i<errors.length; i++) {
numError = i + 1;
msg += "\n" + numError + ". " + errors[i];
}
//alert(msg);

//document.getElementById('fieldIdbname').innerHTML= 'Message';
return false;
}

return true;
}

function checkLength(text, min, max){
min = min || 1;
max = max || 10000;

if (text.length < min || text.length > max) {
return true;
}
return false;
}
</script>

I need to validate user inputs.
I know how to show errors through ALERT boxes.
All i need to show these below the textboxes.
i googled a lot but couldn't find one regarding the same.
I am a niebie to Javascript.
 
Sheriff
Posts: 67618
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You might want to adopt jQuery and then try its Validator plugin.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic