Solved

Simple Javascript validation

Posted on 2004-03-28
6
265 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
Industry Leaders: 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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

Suggested Solutions

Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
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…

733 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