JavaScript中的常见错误消息
Common error message in JavaScript
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,那么你的方法就可以了。我会让它成为一个更通用的验证方法,它会检查您拥有的每个验证规则,如果失败,则将消息添加到数组中。所有检查完成后,如果数组的计数大于零,则输出完整的错误列表。
相关文章:
- jQuery在输入下验证post错误消息
- 在AngularJs中隐藏默认错误消息
- 如果用户输入的长度小于最小长度,则显示错误消息
- 使用ASP.NET CustomValidator避免重复的错误消息
- KOValidation在错误消息中获取可观察值、$index()、$data等
- 更改精细上载中的错误消息
- 如何在页面刷新时隐藏错误消息
- 如何显示错误消息
- 滑块未显示,控制台中没有错误消息
- 表单提交没有'如果为空,则不会显示错误消息
- 为什么我的Alexa技能测试显示正确的lambda输出,但在开发人员控制台中测试时却给出错误消息
- 隐藏错误消息“;未选择文件”;asp:fileUpload的文本
- 登录时显示自定义错误消息
- 如何仅在存在最小值和最大值时才显示错误消息
- 通过 AJAX 检索 Blob 时处理错误消息
- Javascript 错误:消息:预期的“)”
- 如何将错误消息从 meteor 服务器传递到客户端
- 为什么 Angularjs 总是显示错误消息
- 我如何让我的 var 错误消息输出是使用字段还是字段,具体取决于留空的字段数
- 将错误消息 JSON 传递到另一个页面模板