XCeptable
XCeptable

Reputation: 1267

javascript validation to check @ at start of user input: not email validation

I have to check whether a form field contains '@' at start of user input & is it contains it at all. It works fine for checking if its at start of the string. But when I add checking whether input contains '@' at all or not. It fails. Here is my code

  function email_valid(field) 
    { 
      var apos=field.update.value;
          apos=apos.indexOf('@');
            if (apos>0 ||((apos.contains('@')== 'FALSE')))
              { alert('plz enter valid input');
                return false;
              }
            else 
            { return true; }
    }

EDIT

This function in this form is checking both if @ is at 1st place & 2ndly is it in the input at all or not.

   function @_valid(field) 
        { 
          var ref=field.update.value;// I needed ref 4 other things
          var apos=ref.indexOf('@');
          if (apos>=0 )
             {
               if (apos==0)
                   {
                    return true;
                   }
                 else { field.t_update3.value=""; 
                        alert('plz enter a valid refernce');
                        return false;
                      }
                     }
                   else { field.t_update3.value=""; 
                         alert('plz enter a valid refernce');
                         return false;
          }         }

Upvotes: 0

Views: 1063

Answers (9)

scunliffe
scunliffe

Reputation: 63588

Update: revised answer based on comments. code below will only return true if the value contains an "@" symbol at the first character.

If this is a JavaScript question, then this should be fine.

function email_valid(field){ 
  var apos=field.update.value;
  if(apos.indexOf('@') != 0){
    alert('plz enter valid input');
    return false;
  } else {
    //field contains an '@' at the first position (index zero)
    return true;
  }
}

That said, your parameter "field" if it actually refers to an input field element, should only require this code to get the value (e.g. I'm not sure where the ".update" bit comes into play)

var apos = field.value;

I would also rename this function if it isn't doing "email validation" to something a little more appropriately named.

Upvotes: 0

user166390
user166390

Reputation:

Consider:

var apos = value.indexOf('@');
if (apos >= 0) {
  // was found in string, somewhere
  if (apos == 0) {
    // was at start
  } else {
    // was elsewhere
  }
} else {
  // not in string
}

and

var apos = value.indexOf('@');
if (apos == 0) {
  // was at start
} else if (apos > 0) {
  // was elsewhere
} else {
  // not in string
}

Upvotes: 1

Chris Lloyd
Chris Lloyd

Reputation: 12308

Learn about Regular expressions if you haven't already. Then lookup Javascript's String#match. There is no need to find wether the input starts with an "@" as if it contains an "@" that will also return true if the "@" is at the start of the string.

Also, for free, return true and return false are generally bad style. Just return the thing you passed to if (that evaluates to a boolean).

All in all:

function validate_input(str) {
    return str.match(/@/);
}

I reccomend passing the function a string (field.value or some-such) rather than the field itself as it makes it more generic.

Upvotes: 0

SLaks
SLaks

Reputation: 887365

In the second if condition, apos is a number, not a string.

You're trying to write

if (field.update.value.charAt(0) == '@' && field.update.value.indexOf('@', 1) < 0)

Upvotes: 0

sholsinger
sholsinger

Reputation: 3088

apos will be -1 if the string was not found. So your code should be as follows:

function email_valid(field) 
{ 
  var apos=field.value;
      apos=apos.indexOf('@');
        if (apos<=0) // handles '@' at the beginning and not in string at all.
        {
            alert('plz enter valid input');
            return false;
        }
        else 
        { return true; }
}

I also changed your initial assignment to remove the .update portion as that would cause it to fail when field is a reference to an input.

Upvotes: 0

matthewpavkov
matthewpavkov

Reputation: 2928

Try:

function email_valid(field) {
    //var apos=field.update.value;
    var apos = field;
    //apos=apos.indexOf('@');
    apos = apos.indexOf('@');
    if( (apos < 0) ) {
        //alert('plz enter valid input');
        alert('false');
    } else {
        alert('true');
    }
}
email_valid('blah');

Checks for @ anywhere. Or, if you want to check for @ just at the beginning, change if( (apos < 0) ) { to if( (apos == 0) ) {. Or, if you want to make sure it's not at the beginning, then if( (apos > 0) ) {.

Upvotes: 0

Adam
Adam

Reputation: 44929

function email_valid(field) 
    { 
      var fieldValue =field.update.value;
      var apos = apos.indexOf('@');
            if (apos > 0 || apos < 0)//could also use apos !== 0
              { alert('plz enter valid input');
                return false;
              }
            else 
            { return true; }
    }

apos is the value returned by indexOf, it will be -1 if there is no @ in the user input. It will be 0 if it is the first character. It will be greater than 0 if the user input contains an @ . JavaScript has no contains method on a String.

Upvotes: 0

Pointy
Pointy

Reputation: 413720

Why not just

if (apos !== 0) { /* error; */ }

The "apos" value will be the numeric value zero when your input is (as I understand it) valid, and either -1 or greater than 0 when invalid.

This seems like a strange thing to make a user of your site do, but whatever. (If it's not there at all, and it must be there to be valid, why can't you just add the "@" for the user?)

Upvotes: 1

Aaron Hathaway
Aaron Hathaway

Reputation: 4315

You can just check to make sure that apos is greater than -1. Javascript's indexOf() will return the current index of the character you're looking for and -1 if it's not in the string.

edit Misread a bit. Also make sure that it's not equal to 0, so that it's not at the beginning of the string.

Upvotes: 0

Related Questions