根据某些条件验证用户输入,而不是将输入输入到数组中,需要最基本的解决方案
Validating user input to certain criteria and than entering the input into an array, Most basic solution possible is required
大家好。我正在尝试找到一种解决方案,根据某些规范检查用户输入,如果它们有效,则将它们输入到数组中。数组没有限制,因此长度等是未知的。程序应该实现的所有细节都显示在下面的代码中。我只需要弄清楚如何测试输入,如果它是有效的,则将其放入要存储的数组中。我需要解决方案是基本的,而不是先进的,因为我才刚刚开始。
请帮忙。:)
/**
* To capture valid user inputs for calculation
*/
while (confirm('Do you want to continue?') == true)
{
//Ask user input for course code
CourseCode = prompt('Enter Your Course Code like CSC1401');
//Ask user input for grade
CourseGrade = prompt('enter your grade for this course 0-7');
// Check if the user inputs are valid:
// A: the course code is in valid format: (i) 7 characters; (ii) first 3 are letters in uppercase; (iii) last 4 are digits;
// B: the grade is a valid number: (i) a number; (ii) in the range of 0-7;
// C: a new course code that hasn't been recorded yet;
//<<YOUR CODE GOES HERE>>
if (CourseCode == CourseArray[CourseArray.indexOf(CourseCode)])
{ alert (CourseCode + 'Error C: Duplicate Record')
}
if ( CourseCode.slice(0,3) != CourseCode.slice(0,3).toUpperCase() || CourseCode.length != 7 || isNaN(CourseCode.slice(3)))
{alert(CourseCode +' Error A: Invalid Course Code');
}
if (CourseGrade < 0 || CourseGrade > 7 || isNaN(CourseGrade) )
{alert(CourseGrade +' Error B: Invalid Grade');
}
else
{CourseArray.push(CourseCode);
GradesArray.push(parseInt(CourseGrade));
}
//if the course and grade are valid then store them somewhere for future process
//if invalid then display to user an error message corresponding to the problem type(s).
//Error messages: 'Problem Type A - Invalid course code; '
// 'Problem Type B - Invalid grade; '
// 'Problem Type C - Duplicate Record.'
//Note that
// 1. a combination of multiple error messages is possible if more than one error type is captured;
// 2. the error messages don't need to go for further details.
//<<YOUR CODE GOES HERE>>
}
以下是输入的测试:
var CourseCode = 'AAA4331'; // from prompt
var CourseGrade = '7'; // from prompt
var CourseCode_pat = /[A-Z][A-Z][A-Z]'d'd'd'd/;
var CourseGrade_pat = /[0-7]/;
if(CourseCode.match(CourseCode_pat) && CourseGrade.match(CourseGrade_pat))
alert('passed')
这是
家庭作业,对吧?如果你找到自己的答案,对你更好。这是一个开始:使用子字符串方法选取输入字符串的一部分并对其进行测试。
str.substring(1, 2)
http://www.w3schools.com/jsref/jsref_substring.asp
另一种选择是使用正则表达式
http://www.w3schools.com/jsref/jsref_obj_regexp.asp
但也许事实并非如此...
-
按照惯例,在命名javascript变量时使用lowerCamelCase是最常见/最佳实践。
-
您将课程代码和成绩存储在两个单独的数组中,您可能想查看 JSON 对象或地图,以 { 课程代码:课程等级 } 的格式存储数据
-
您需要第三个对象来存储错误消息,而不是像您已经在执行的 alert(( 处理错误消息 [即:数组]
var errorMessages = []; errorMessages.push("error message 1")
-
要验证输入,您可以查看正则表达式模式/匹配。
-
你真的应该用
var
声明你的变量,以避免有全局变量
[如果是这样的话,请将此问题标记为"家庭作业"]
相关文章:
- $q.all当输入数组中的一项不是promise时,Typescript检查器失败
- 你能提交一个带有文本输入数组的表单吗
- 使用 jQuery 和 Ajax 发送输入数组
- $.when.apply 根据输入数组的长度返回具有不同结构的结果
- PHP只读取动态创建的输入数组的第一个值
- 使用jquery设置输入数组值
- 使用 javascript 验证输入数组
- 角度 UI 预测文本:如何输入未包含在输入数组中的数据
- 将动态输入索引到其他动态输入数组
- 循环遍历输入数组以设置值,“无法设置未定义的属性值”
- 在输入数组元素时在 html 页面中显示数组元素
- Javascript 隐藏输入数组
- 编码然后在 base64 中解码不会输出我的输入数组缓冲区
- 将隐藏的输入数组传递给 jQuery load()
- 将输入数组中的特定输入值发送到 JavaScript
- 在输入数组的 split() 和子字符串 () 之后的文本区域中显示
- Jquery 添加未传递给 php 的输入数组字段
- Javascript函数显示用户输入数组
- 从输入数组中获取sum's元素
- 如何在文本输入数组中插入字符串