Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/473.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JavaScript中的常见错误消息_Javascript - Fatal编程技术网

JavaScript中的常见错误消息

JavaScript中的常见错误消息,javascript,Javascript,AddPatient是一个对象,我正在发送请求之前检查它是否为空 AddPatient = {}; AddPatient.Firstname = FirstNameValue || PatientModel.errorMsg('FirstName',FirstNameValue); AddPatient.LastName = LastNameValue || PatientModel.errorMsg('LastName',LastNameValue); 我有一个表单,其中有FirstNam

AddPatient是一个对象,我正在发送请求之前检查它是否为空

AddPatient = {};

AddPatient.Firstname = FirstNameValue || PatientModel.errorMsg('FirstName',FirstNameValue);
AddPatient.LastName = LastNameValue || PatientModel.errorMsg('LastName',LastNameValue);
我有一个表单,其中有FirstName和LastName字段,我必须检查它不应该为空。我想要一个javascript中可以工作的单一函数


这是正确的方法吗?

我建议使用。它将允许您进行一系列客户端验证。包括所需的验证


如果您只需要纯javascript,那么您的方法就可以了。我会使它成为一个更通用的验证方法,它会检查您拥有的每个验证规则,如果失败,则将消息添加到数组中。完成所有检查后,如果阵列计数大于零,则输出完整的错误列表

我可以看到您的代码中有几个问题

  • errorMsg()
    的预期参数与调用方式不匹配
  • 第二个警报()中出现语法错误
  • if
    语句中的错误表达式

errorMsg()
的预期参数与调用方式不匹配
errorMsg()
函数需要三个参数,但一次只传递两个:

PatientModel.js

errorMsg: function(title,FirstNameValue,LastNameValue) {
        if(FirstNameValue === undefined || FirstNameValue === ' ' && LastNameValue === undefined || LastNameValue = ' ') {
          alert('FirstName and LastName are missing');
          return false;
          } else {
          alert(+title 'is missing');
                        return false; 
          } 
        }
如果确实要在
errorMsg()
中使用这两个值,则每次都需要按函数所期望的顺序传递这两个值:

errorMsg: function(title,FirstNameValue,LastNameValue) {
    ....
}

... and then ....

.errorMsg('FirstName',FirstNameValue);
.errorMsg('FirstName',LastNameValue);
第二个警报()中出现语法错误 这很容易修复,可能只是一个打字错误

PatientModel.errorMsg('FirstName',FirstNameValue,LastNameValue);
PatientModel.errorMsg('LastName',FirstNameValue,LastNameValue);
// This is weird code, but it'll work
你想要的是:

alert(+title 'is missing');
      ^      ^_There's something missing here.
      |_This will only try to convert title to a number
if
语句中的错误表达式 这不会像您预期的那样工作,因为
&&
的优先级高于
|
,这意味着表达式的计算结果如下:

if(FirstNameValue === undefined || FirstNameValue === ' ' && LastNameValue === undefined || LastNameValue = ' ') {
您需要使用括号来修复优先级:

if (
     FirstNameValue === undefined
 || (FirstNameValue === ' ' && LastNameValue === undefined)
 ||  LastNameValue = ' '
) {
实际上,这是不相关的,因为表达式可以简化为:

if( (FirstNameValue === undefined || FirstNameValue === ' ') && (LastNameValue === undefined || LastNameValue = ' ') ) {
// If these values are taken from a form input, they are guaranteed to be strings.
if(FirstNameValue.length === 0 && LastNameValue.length === 0) {
或者更好,像这样:

if( (FirstNameValue === undefined || FirstNameValue === ' ') && (LastNameValue === undefined || LastNameValue = ' ') ) {
// If these values are taken from a form input, they are guaranteed to be strings.
if(FirstNameValue.length === 0 && LastNameValue.length === 0) {
我将如何修复你的代码 如果我没有提供您的代码的正确版本,那么这将是一个不完整的答案,所以就这样吧。请注意,我将信息的填写和验证分为两个步骤

// Uses regular expressions to checks if string is not whitespace only
var WHITESPACE = /^\s*$/;
if( WHITESPACE.test(FirstNameValue) && WHITESPACE.test(FirstNameValue)){
编辑:另一种可能更好的方法。唯一的区别是我们现在将validate()作为患者对象的方法编写:

PatientModel.js :

validate: function(patient){
    var WHITESPACE = /^\s*$/;
    var errors = [];

    if( WHITESPACE.test(patient.FirstName) ){
        // No first name
        if( WHITESPACE.test(patient.LastName) ){
            // No last name either
            errors.push('FirstName and LastName are missing');
        }
        else {
            // Only first name missing
            errors.push('FirstName is missing');
        }
    }
    else if( WHITESPACE.test( patient.LastName) ){
        // Only last name missing
        errors.push('LastName is missing');
    }

    // Returns array of errors
    return errors;
}



Your other code:

AddPatient = {};
AddPatient.Firstname = FirstNameValue; 
AddPatient.LastName = LastNameValue;

errors = PatientModel.validate(AddPatient);
if( errors.length != 0 ){
    alert('You have the following errors:\n' + errors.join('\n'));
}

@John Cooper您可以不使用Jquery来完成它。请参见编辑以回答(您不想使用它的任何原因>)不,他的方法不好。if中的表达式无法按预期工作,因为
&&
|
之间存在相对优先级,并且第二个警报中存在语法错误。@John,是的,我应该编写
测试
,而不是
匹配
。很抱歉。我已经编辑了答案。