gpt4 book ai didi

javascript - 提交前验证文本框输入(用于 fasta 格式)

转载 作者:行者123 更新时间:2023-11-29 21:49:31 26 4
gpt4 key购买 nike

我想验证在 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 提供了您需要的一切。

关于javascript - 提交前验证文本框输入(用于 fasta 格式),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29937157/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com