我的建议是,最好用“ \ n”拆分多行字符串,然后将原始字符串的拆分连接起来,成为单行且易于操作。
<textarea class="form-control" name="Body" rows="12" data-rule="required"
title='@("Your feedback ".Label())'
placeholder='@("Your Feedback here!".Label())' data-val-required='@("Feedback is required".Label())'
pattern="^[0-9a-zA-Z ,;/?.\s_-]{3,600}$" data-val="true" required></textarea>
$( document ).ready( function() {
var errorMessage = "Please match the requested format.";
var firstVisit = false;
$( this ).find( "textarea" ).on( "input change propertychange", function() {
var pattern = $(this).attr( "pattern" );
var element = $( this );
if(typeof pattern !== typeof undefined && pattern !== false)
{
var ptr = pattern.replace(/^\^|\$$/g, '');
var patternRegex = new RegExp('^' + pattern.replace(/^\^|\$$/g, '') + '$', 'gm');
var ks = "";
$.each($( this ).val().split("\n"), function( index, value ){
console.log(index + "-" + value);
ks += " " + value;
});
//console.log(ks);
hasError = !ks.match( patternRegex );
//debugger;
if ( typeof this.setCustomValidity === "function")
{
this.setCustomValidity( hasError ? errorMessage : "" );
}
else
{
$( this ).toggleClass( "invalid", !!hasError );
$( this ).toggleClass( "valid", !hasError );
if ( hasError )
{
$( this ).attr( "title", errorMessage );
}
else
{
$( this ).removeAttr( "title" );
}
}
}
});
});