Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Backslash problem...

Status
Not open for further replies.

GUJUm0deL

Programmer
Jan 16, 2001
3,676
US
I'm trying have a backslash (/) appear in a textarea when the page is loaded, then the user can add up to ten characters, then another backslash (/) appears in that textfield. Looks like this: /1234567890/
My script does this and I also give an alert message if the user types in more then 10 characters, but I also want the alert to come if the user DOESN'T type in ten characters. In other words, the user MUST type in ten characters no more or no less otherwise they get an alert message. i tired changing the if(line.length>11) to
if(line.length<=11) and also if(!line.length==11) nothing works. Anyone know what I can do? I uploaded the file to this site, so you can see it in action.

 
this worked for me (in IE5.5):

<script>
function checkNums()
{
if(benluc.value.length!=11)
{
alert(&quot;You must type in 10 characters!&quot;);
benluc.focus();
benluc.value = &quot;/&quot;
}
else
{
benluc.value+=&quot;/&quot;
}
}
</script>

<input onblur=&quot;checkNums()&quot; type=&quot;text&quot; id=&quot;benluc&quot; value=&quot;/&quot;>

to make it work in NS, you just have to put it in a form and reference the form when you reference the text box... jared@aauser.com
 
hi jaredn, it still doesnt work right. now i get the alert message after the user types in one character, i need it to give an alert only if the user types less then 10 characters and tries to take the focus off the textarea. the way it works now, it gives an alert after every character entered, not giving the user the chance to enter more then one character. i did a little change to your script. do you think that moving the alert messages outside the if/else statements might work??

<script>
function checkNums()
{
if(document.form1.benluc.value.length<=11)
{
alert(&quot;You must type in 10 characters!&quot;);
document.form1.benluc.focus();
document.form1.benluc.value = &quot;/&quot;
}
else
{
document.form1.benluc.value+=&quot;/&quot;
}
}
</script>
<form method=&quot;post&quot; name=&quot;form1&quot;>
<p>
<input value=&quot;/&quot; name=&quot;benluc&quot; onKeyUp=&quot;checkNums(document.form1.benluc.value);&quot;>
</p>
</form>
 
notice that mine uses an onblur event instead of onkeyup. is that ok? jared@aauser.com
 
Try this:

<HTML>
<title>backslash</title>
<meta name=&quot;AUTHOR&quot; content=&quot;Ketan Mehta&quot;>
<meta name=&quot;description&quot; content=&quot;Loads page with a backslash, the user then can input up to 10 charactyers [this can be edited] and another backslash appears when the focus is off the textarea.&quot;>
<HEAD>
<SCRIPT LANGUAGE=&quot;javascript&quot;>
<!--
function check_length(vInput) {
// Call strip function
stripNonAlpha(vInput);

// Shorten the value of the string if it is shorter then 10
if (vInput.value.length != 10) {
alert(&quot;You must type in 10 characters!&quot;);
vInput.value = vInput.value.substring(0,10); // Shorten the field to 10 chars
vInput.focus(); // Return focus to field

}else{ // If it is equal to 10, then add the &quot;/&quot; string &quot;/&quot;;
vInput.value = &quot;/&quot; + vInput.value + &quot;/&quot;;
}
}

// Replace out any non-alphanumeric characters (strip the '/')
function stripNonAlpha(vInput){
vInput.value = vInput.value.replace(/\W/g,'');
}

//-->
</SCRIPT>
</HEAD>

<BODY>
<FORM NAME=&quot;form1&quot;>
<INPUT TYPE=&quot;Text&quot; NAME=&quot;txt1&quot; VALUE=&quot;&quot; onFocus=&quot;stripNonAlpha(this)&quot; onBlur=&quot;check_length(this)&quot;>
</FORM>

</BODY>
</HTML>
 
Hi tleish, I tried your method also, and the alert is right, but this way the backslashes only appear when the focus is off. I need the first backslash to appear when the form is loaded, and the last one after the 10 characters are added. I tried to play around with your code but I either get the alerts to work right or the backslashes to work right. You know what else I can do??
 
jaredn
Hiya, the onBlur doesn't really matter, I can use that and the onKeyPress, both ways is fine...but it still gives the alert after each character...i tried tleish's method and the alert works fine, but the backslash doesn't appear right...
 
Try this one..

Matt.

<HTML>
<title>backslash</title>
<meta name=&quot;AUTHOR&quot; content=&quot;Ketan Mehta&quot;>
<meta name=&quot;description&quot; content=&quot;Loads page with a backslash, the user then can input up to 10 charactyers [this can be edited] and another backslash appears when the focus is off the textarea.&quot;>
<HEAD>
<SCRIPT LANGUAGE=&quot;javascript&quot;>
<!--

function check_length(line) {

if (line.length>11) {
line=line.substring(0,11);
alert(&quot;please 10 characters only&quot;);
document.form1.txt1.value=line;
document.form1.txt1.focus();
}

if (line.length==11){
finish();
}
}

function check_done(line) {
if (line.length<11) {
line=line.substring(0,11);
alert(&quot;please 10 characters only&quot;);
document.form1.txt1.value=line;
document.form1.txt1.focus();
}
}

function finish() {
document.form1.txt1.value+=&quot;/&quot;;
}
//-->
</SCRIPT>
</HEAD>

<BODY>
<FORM NAME=&quot;form1&quot;>
<INPUT TYPE=&quot;Text&quot; NAME=&quot;txt1&quot; VALUE=&quot;/&quot; onKeyUp=&quot;check_length(document.form1.txt1.value);&quot;
onBlur=&quot;check_done(document.form1.txt1.value);&quot;>

</FORM>

</BODY>
</HTML>
 
How's this:

<HTML>
<title>backslash</title>
<meta name=&quot;AUTHOR&quot; content=&quot;Ketan Mehta&quot;>
<meta name=&quot;description&quot; content=&quot;Loads page with a backslash, the user then can input up to 10 charactyers [this can be edited] and another backslash appears when the focus is off the textarea.&quot;>
<HEAD>
<SCRIPT LANGUAGE=&quot;javascript&quot;>
<!--
function check_length(vInput) {
// Call strip function
stripNonAlpha(vInput);

// Shorten the value of the string if it is shorter then 10
if (vInput.value.length != 10) {
alert(&quot;You must type in 10 characters!&quot;);
vInput.value = vInput.value.substring(0,10); // Shorten the field to 10 chars
vInput.focus(); // Return focus to field

}else{ // If it is equal to 10, then add the &quot;/&quot; string &quot;/&quot;;
vInput.value += &quot;/&quot;;
}
}

// Replace out any non-alphanumeric characters (strip the '/')
function stripNonAlpha(vInput){
vInput.value = &quot;/&quot; + vInput.value.replace(/\W/g,'');
}

//-->
</SCRIPT>
</HEAD>

<BODY>
<FORM NAME=&quot;form1&quot;>
<INPUT TYPE=&quot;Text&quot; NAME=&quot;txt1&quot; VALUE=&quot;/&quot; onBlur=&quot;check_length(this)&quot;>
</FORM>

</BODY>
</HTML>
 
Hiya, YES!! it works, both versions: MatthewP and tleish's...
thanks so much...and lookin at ure codes i figured out wha was wrong with mines...
thanks...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top