javascript验证空间 [英] javascriptvalidation for space
本文介绍了javascript验证空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
嗨
我想制作一个JavaScript函数来提示用户在25个字符后放置空格.这意味着用户有必要在总长度为58的文本框中放置25个字符后的空格.
等待中
到现在为止,我一直在使用此
Hi
I want to make a javascript function that will prompt user to put space after 25 characters.Means it will be necessary for user to put space after 25 characters in a textbox which has total length of 58.
Waiting
Till now m using this
function nospaces(t){
if(t.value.match(/\s/g)){
alert('Sorry, you are not allowed to enter any spaces');
t.value=t.value.replace(/\s/g,'');
}
}
<input type="text" maxlength="58" name ="textbox" id="textbox" onkeyup="nospaces(this)">
但是它的作用是不允许有任何空间
ANy Help
but what it does ,it does not allow any space
ANy Help
推荐答案
function isValid(e){
var value = e.value;
// check if value is null
if(value==null) {
alert('value is null');
return false;
}
// check if value is exact 58 characters long
if(value.length!=58) {
alert('value is not 58 characters long');
return false;
}
// check if character 25 is a space
if(value.substring(25,26)!=' ') {
alert('character 26 is not a space');
return false;
}
return true;
}
<input type="text" maxlength="58" name ="textbox" id="textbox" onblur="isValid(this)">
尝试以下操作:
按钮的aspx标记
Try this:
the aspx markup for button
<asp:TextBox ID="TextBox1" runat="server" onkeypress="return checkSpace(this);" Width="485px" MaxLength="58"></asp:TextBox>
和JavaScript来实现您想要的
and the javaScript to achieve what you want
function checkSpace(sender)
{
var data = sender.value;
if(data.length == 25 && data.substring(25,26) != ' ')
{
alert('please enter space now');
return false;
}
}
我对它进行了最终测试,并且工作正常.让我知道它是否对您也有用.如果没有,我会尝试完善答案.
I tested it on my end and it is working fine. Let me know if it works on your end too. if not I will try to refine my answer.
在正则表达式中查看这些文章
Article1 [ Article2 [ Article3 [第4条 [
look these articles on regular expression
Article1[^]
Article2[^]
Article3[^]
Article4[^]
这篇关于javascript验证空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文