Jump to content

[SOLVED] Validation Function


ballouta

Recommended Posts

Hello

 

I googled for a function that validates if the username or password fields submitted are between 5 and 12 characters but my form was damaged. I give up.

Please Help.

This is the .js file that I have and it working for the Full Name (fname), and email.

I wanna make sure that the subscriber is writing characters between 4 and 12.

would you please give me a fucntion that i can include in my js file ?

 

function isReady() {

// check if the First Name is valid
    if (isProper(frm.fname.value) == false) {
        alert("Please enter your First Name");
        return false;
    }

// check if the email address is valid
    if (isEmail(frm.email.value) == false) {
        alert("Please enter a valid email address");
        return false;
    }

        // check if the country is selected
    if (isProper(frm.country.value) == false) {
        alert("Please select a country");
        return false;
     }

// check if the User Name is valid
    if (isProper(frm.user.value) == false) {
        alert("Please enter a valid username");
        return false;
     }

// check if the Password is valid
    if (isProper(frm.pass.value) == false) {
        alert("Please enter a correct password");
        return false;
     }


    return true;
    /**********************************************/
}

function isEmail(string)
{
if (string.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) != -1)
        return true;
    else
        return false;
        
   if (!string) return false;
   var iChars = "*|,\":<>[]{}`\';()&$#%";

   for (var i = 0; i < string.length; i++) {
      if (iChars.indexOf(string.charAt(i)) != -1)
         return false;
   }
   return true;
      
}     

/**********************************************/                 
function isProper(string) {
  if (string.search(/^\w+( \w+)?$/) != -1)
        return true;
    else
        return false;
        
   if (!string) return false;
   
   var iChars = "*|,\":<>[]{}`\';()@&$#%";

   for (var i = 0; i < string.length; i++) {
      if (iChars.indexOf(string.charAt(i)) != -1)
         return false;
   }
   return true;

}

Link to comment
Share on other sites

You can use a function like this:

<script language="Javascript">
function validate(text)
{
if (text.length > 3 && text.length < 13)
	return true;
else
	return false;
}
</script>

 

You can either change the return code to do what you want it to do or call it like:

if (validate(textbox1.value)) alert("valid");

Link to comment
Share on other sites

BY the way, I believe there is a problem with the isProper function you posted above:

function isProper(string) {
  if (string.search(/^\w+( \w+)?$/) != -1)
        return true;
    else
        return false;
        
   if (!string) return false;
   
   var iChars = "*|,\":<>[]{}`\';()@&$#%";

   for (var i = 0; i < string.length; i++) {
      if (iChars.indexOf(string.charAt(i)) != -1)
         return false;
   }
   return true;

}

The first IF/ELSE statemetn will either return true or false, so everything after that is never run. This should fix it if I am reading it correctly:

function isProper(string) {
   if (string.search(/^\w+( \w+)?$/) == -1) return false;
        
   if (!string) return false;
   
   var iChars = "*|,\":<>[]{}`\';()@&$#%";

   for (var i = 0; i < string.length; i++) {
      if (iChars.indexOf(string.charAt(i)) != -1)
         return false;
   }
   return true;

}

 

Also, you could replace the for loop with a single regex pattern.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.