如果字段不需要验证,则javascript验证无效

时间:2014-04-04 12:27:03

标签: javascript titanium-mobile

我有手机短信,我给了默认值一些文字。

该文本字段不是必需的,但如果用户输入该号码,则应检查精灵数字

我的问题是这个字段要求验证

文本归档代码

     var cellnotextfield = Titanium.UI.createTextField({
    //borderStyle : Ti.UI.INPUT_BORDERSTYLE_ROUNDED,
    value : 'Cellphone Number',
    backgroundColor : '#32302D',
    color : 'white',
    //backgroundImage : 'none',
    keyboardToolbarHeight : 30,
    borderRadius : '7%',
    paddingLeft : 10,
    width : '98%',
    height : '20%',
    top : '4%',

    //backgroundColor:'green'
});


 and validation code
       if (namesurnametextfield.value == '' || namesurnametextfield.value =='Name & Surname*' ) {
        alert("Please enter the name or surname");
        return;
        //isvalid=false;
    } else if (!namesurnametextfield.value.match(/^[a-zA-Z ]*$/)) {
        alert('Please enter only alphabets');
        isvalid = false;
        return;
        //return 0;
    } else if (emailtextfield.value == '' || emailtextfield.value =='Email Address*') {
        alert('Please enter  your email');
        return;
        //return 0;
    } else if (!emailtextfield.value.match(/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/)) {
        alert('Please enter valid email');
        return;
        //return 0;
    } else if (cellnotextfield.value == '' || cellnotextfield.value =='Cellphone Number') {

        alert("dont do any thing");
    }else if (!cellnotextfield.value.match(/^\d{11}$/)) {

            alert('Please enter only elven eigits');

        }
     else if (displaynametextfield.value == '' || displaynametextfield.value =='Display Name*') {
        alert('Please enter display name');
        return;
        //return 0;
    } else if (!displaynametextfield.value.match(/^[a-zA-Z]+$/)) {
        alert('Please enter only alphabets');
        return;
        //return 0;
    }

1 个答案:

答案 0 :(得分:1)

问题是您在条件内输入验证,而它应该是else if

else if (!(cellnotextfield.value == '' || cellnotextfield.value =='Cellphone Number') && !cellnotextfield.value.match(/^\d{11}$/)) {

    alert('Please enter only elven eigits');
    return;
}