Solved

Simple Javascript validation

Posted on 2004-03-28
6
266 Views
Last Modified: 2006-11-17
How do I code javascript, that can validate whether the visitor fills in the regsistration number and if he does, then it should be 7 digits in length. My current code is as follows, but it doesn't work.


if (theForm.regNo.value == "" && theForm.regNo.value.length < 7)
  {
    alert("Please enter your valid Registration Number.");
    theForm.regNo.focus();
    return (false);
  }
0
Comment
Question by:zamorin
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
6 Comments
 
LVL 63

Accepted Solution

by:
Zvonko earned 50 total points
ID: 10702078
Like this:

  if (theForm.regNo.value.length != 7){
    alert("Please enter your valid Registration Number.");
    theForm.regNo.focus();
    return (false);
 }

0
 
LVL 5

Author Comment

by:zamorin
ID: 10702122
That is not  accurate as it doesn't allow 'no registration number'. The proper one is as follows:

  if (!theForm.regNo.value == "" && theForm.regNo.value.length < 7)
  {
     alert("Please enter your valid Registration Number.");
     theForm.regNo.focus();
     return (false);
  }

Points for you. Thanks!

PS: Do you have any simple email validation script that checks for '@' and '.' in their email field?

0
 
LVL 63

Expert Comment

by:Zvonko
ID: 10702175
Let me first finish this:

if (theForm.regNo.value != "" && theForm.regNo.value.length != 7){
   alert("Please enter your valid Registration Number.");
   theForm.regNo.focus();
   return (false);
 }

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 63

Expert Comment

by:Zvonko
ID: 10702191
And for email check this:

  if(!theForm.EmailAddress.value.match(/^\w+([\.\-]\w+)*\@\w+([\-\.]\w+)*\.[a-z]{2,4}$/i)){
    alert("Wrong email: "+theForm.EmailAddress.value);
    theForm.EmailAddress.select();
    theForm.EmailAddress.focus();
    return false;
  }


0
 
LVL 5

Author Comment

by:zamorin
ID: 10702346
works fine, thanks again.

0
 
LVL 63

Expert Comment

by:Zvonko
ID: 10702402
You are welcome.
0

Featured Post

Transaction Monitoring Vs. Real User Monitoring

Synthetic Transaction Monitoring Vs. Real User Monitoring: When To Use Each Approach? In this article, we will discuss two major monitoring approaches: Synthetic Transaction and Real User Monitoring.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

717 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question