我想验证HTML页面上的文本框输入是否为sum = 0
for num in range (2, 10):
for i in range (2, int(num ** 0.5) + 1):
if not num % i:
break
else:
sum += num
格式,是否在提交数据之前只包含一个序列。
我知道PHP和JavaScript的一点点想法。 我不认为用PHP可以。
答案 0 :(得分:2)
为了使用JavaScript验证它,您可以使用以下函数:
/*
* Validates (true/false) a single fasta sequence string
* param fasta the string containing a putative single fasta sequence
* returns boolean true if string contains single fasta sequence, false
* otherwise
*/
function validateFasta(fasta) {
if (!fasta) { // check there is something first of all
return false;
}
// immediately remove trailing spaces
fasta = fasta.trim();
// split on newlines...
var lines = fasta.split('\n');
// check for header
if (fasta[0] == '>') {
// remove one line, starting at the first position
lines.splice(0, 1);
}
// join the array back into a single string without newlines and
// trailing or leading spaces
fasta = lines.join('').trim();
if (!fasta) { // is it empty whatever we collected ? re-check not efficient
return false;
}
// note that the empty string is caught above
// allow for Selenocysteine (U)
return /^[ACDEFGHIKLMNPQRSTUVWY\s]+$/i.test(fasta);
}
来源:http://www.blopig.com/blog/2013/03/a-javascript-function-to-validate-fasta-sequences/
但请注意,您应该使用php
在服务器端执行相同的检查。语言非常相似,php提供了您需要的一切。
答案 1 :(得分:0)
这是我根据自己的需要修改的脚本,并且在我的情况下它运行得非常好。
----- java script ------
function validate(){
var seq = $("#protein_seq").val();
if (!seq) {
alert("No input");
// check there is something first of all
return false;
}
seq = seq.trim();
// split on newlines...
var lines = seq.split('\n');
// check for header
if (seq[0] == '>') {
// remove one line, starting at the first position
lines.splice(0, 1);
if (lines[0] ==undefined) {
alert("Please enter amino acid sequence in second line");
}
}
else{ alert("First line should start with '>' and amino-acid sequence in next line. Please refer to example data");
//The seq string contains non-DNA characters
return false;}
// join the array back into a single string without newlines and
// trailing or leading spaces
seq = lines.join('').trim();
//Search for charaters that are not G, A, T or C.
if (seq.search(/[^ACDEFGHIKLMNPQRSTUVWY\s]/i) != -1) {
alert("Unspecified amino acid seq");
//The seq string contains non-protein characters
return false;
}
--------- html page ------------------------------------ -
<form action="action.php" method="post" enctype="multipart/form-data" onSubmit="return validate()"><br><textarea align=center type="text" id='protein_seq' name="protein_seq" row='80' cols='100'></textarea><br>
<input id="button1" type="submit" class="myButton" value="Submit" />