提交前验证文本框输入(用于 fasta 格式)
validate text box input (for fasta format) before submission
我想验证 HTML 页面上的文本框输入是否为 fasta
格式,以及它是否只包含一个序列,然后再提交数据。
我知道 PHP 但对 JavaScript 知之甚少。
我认为 php.
不可能
为了使用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 提供您需要的一切。
这是我根据需要修改的脚本,在我的情况下效果很好。
-----java 脚本------
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页数---------------------------- ------
<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" />
我想验证 HTML 页面上的文本框输入是否为 fasta
格式,以及它是否只包含一个序列,然后再提交数据。
我知道 PHP 但对 JavaScript 知之甚少。 我认为 php.
不可能为了使用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 提供您需要的一切。
这是我根据需要修改的脚本,在我的情况下效果很好。
-----java 脚本------
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页数---------------------------- ------
<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" />