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 ot开发者_Go百科her 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;
} }
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
}
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?)
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.
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.
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) ) {
.
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.
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)
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.
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.
精彩评论