Solved

Simple Javascript validation

Posted on 2004-03-28
6
261 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
  • 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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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…

744 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now