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
Solved

Simple Javascript validation

Posted on 2004-03-28
6
264 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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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…

840 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