JavaScript中的常见错误消息

Common error message in JavaScript

本文关键字:错误 消息 常见 JavaScript      更新时间:2023-09-26
AddPatient = {};
AddPatient.Firstname = FirstNameValue || PatientModel.errorMsg('FirstName',FirstNameValue);
AddPatient.LastName = LastNameValue || PatientModel.errorMsg('LastName',LastNameValue);

AddPatient是一个Object,在发送请求之前,我会检查它是否为空。

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; 
          } 
        }

我有一个表格,我有FirstName和LastName字段,我必须检查它不应该是空白的。我想要一个能够工作的javascript函数。

这样做对吗?

我可以在您的代码中看到一些问题。

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

errorMsg()的预期参数与其调用方式不匹配

errorMsg()函数需要三个参数,但一次只传递两个:

errorMsg: function(title,FirstNameValue,LastNameValue) {
    ....
}
... and then ....
.errorMsg('FirstName',FirstNameValue);
.errorMsg('FirstName',LastNameValue);

如果你真的想在errorMsg()中使用这两个值,你需要每次都传递它们,按照函数期望的顺序:

PatientModel.errorMsg('FirstName',FirstNameValue,LastNameValue);
PatientModel.errorMsg('LastName',FirstNameValue,LastNameValue);
// This is weird code, but it'll work

第二个alert()出现语法错误

这很简单,可以修复,可能只是一个拼写错误。

alert(+title 'is missing');
      ^      ^_There's something missing here.
      |_This will only try to convert title to a number

你想要的是:

alert(title + 'is missing');

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) {

或者更好,像这样:

// Uses regular expressions to checks if string is not whitespace only
var WHITESPACE = /^'s*$/;
if( WHITESPACE.test(FirstNameValue) && WHITESPACE.test(FirstNameValue)){

我将如何修复你的代码

如果我没有提供你的代码的正确版本,这将是一个不完整的答案,所以就这样吧。请注意,我将信息的填写和验证分为两个步骤。

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'));
}

编辑:一种不同的,也许更好的方法。唯一的区别是,我们现在将validate((作为Patient对象的一种方法:

>>> PatientModel.js:
var WHITESPACE = /^'s*$/;
// Creates a new empty Patient
function Patient(){
    this.FirstName = '';
    this.LastName = '';
}
// Adds a validate() method to all Patient instances
Patient.prototype.validate: function(){
    var errors = [];
    if( WHITESPACE.test(this.FirstName) ){
        // No first name
        if( WHITESPACE.test(this.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( thisLastName) ){
        // Only last name missing
        errors.push('LastName is missing');
    }
    // Returns array of errors
    return errors;
}


>>> Your other code :
patient = new Patient();
patient.FirstName = FirstNameValue;
patient.LastName = LastNameValue;
errors = patient.validate();
if( errors.length != 0 ){
    alert('You have the following errors:'n' + errors.join(''n'));
}

我建议使用Jquery验证插件来实现此功能。它将允许您进行一系列客户端验证。包括所需的验证。

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