Reputation: 3269
I have some strange restrictions on the following problem. (My friends aren't very good with JavaScript, nor do they understand what happens to the information once it's sent, i.e., they don't understand Java nor what happens on the back end. Consequently, any potential solutions I'm cooking up are trying to avoid using too much of either of those two. Finally, since I'm trying to allow all users to view the page completely, I'm not using too much HTML5, which IE isn't supporting fully just yet.)
I'm trying to do the following: I want to have a form that people can fill out. If they don't fill out a required field I want to notify them without redirecting. Can I use HTML tags in the following way
<input type="text" name="EMAIL" id="EMAIL" value="" required="required" /> ?
I can wrap this form with an onSubmit() function that says,
function onSubmit()
{
for (each_required_field)
{
if (cur_val == null || cur_val == '') return();
}
document.submit();
}
I just don't know if this is possible in HTML and JavaScript. (I mostly know Java, but I don't want to redirect the whole page to handle this. Can I grab the value of the variable EMAIL without having to redirect the client?)
Upvotes: 1
Views: 262
Reputation: 14309
You don't need jQuery. One should learn javascript before they learn jQuery.
<input type="text" name="EMAIL" id="email" value="" required="required" /> <div id="email-err"></div>
function onSubmit()
{
for ( node in document.getElementById('myform').childNodes )
{
if (node.value == '')
{
// add an asterisk to error field next to input
document.getElementById( node.id + "-err" ).innerHTML = "*";
return; //note that return is not a function
}
myForm.submit(); // this is just for illustration
}
You may also want to look into server side and client side email validation regular expressions to make sure it is a valid email address.
Upvotes: 1