Avatar of zamorin
zamorinFlag for Malaysia

asked on 

Simple Javascript validation

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);
  }
JavaScript

Avatar of undefined
Last Comment
Zvonko
ASKER CERTIFIED SOLUTION
Avatar of Zvonko
Zvonko
Flag of North Macedonia image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of zamorin
zamorin
Flag of Malaysia image

ASKER

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?

Avatar of Zvonko
Zvonko
Flag of North Macedonia image

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);
 }

Avatar of Zvonko
Zvonko
Flag of North Macedonia image

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;
  }


Avatar of zamorin
zamorin
Flag of Malaysia image

ASKER

works fine, thanks again.

Avatar of Zvonko
Zvonko
Flag of North Macedonia image

You are welcome.
JavaScript
JavaScript

JavaScript is a dynamic, object-based language commonly used for client-side scripting in web browsers. Recently, server side JavaScript frameworks have also emerged. JavaScript runs on nearly every operating system and in almost every mainstream web browser.

127K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo