Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Javascript
|
Javascript General Javascript discussions.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Javascript section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old February 10th, 2006, 08:13 PM
Registered User
 
Join Date: Feb 2006
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to rungss Send a message via Yahoo to rungss
Default onkeypressevent Location of cursor in the textbox

Hi all This is my first post in this forum
I wanted to create an eventhandler which accepts only time that is it wont accept 12.60
After decimal it accepts only a number between 0 and 59

here is the code that I have written

function checkTime(e, objText)
{
    var key = '';
    var previousContent = objText.value;
    var strCheck = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVW XYZ`~!@#$%^&*()_+[]{};:",<>/?\|-+';
    var strCheck2 = "'";
    var whichCode = (window.Event) ? e.which : e.keyCode;

    key = String.fromCharCode(whichCode); // Get key value from key code

    if(previousContent.indexOf('.') != -1)
        {
            var strAllNum = "0123456789";
            var str6789 = "6789";
            if(key.indexOf('.') != -1)
                return false;//The string already contains a decimal point one more should not be allowed
            if(previousContent.indexOf('.') == previousContent.length - 3)
                if(strAllNum.indexOf(key) != -1)//two places after decimal dont allow any numeric now
                    return false;
            if(previousContent.indexOf('.') == previousContent.length - 1)
                if(str6789.indexOf(key) != -1)//This is the first character after decimal so 6789 wont be allowed
                    return false;
        }

    if (strCheck.indexOf(key) != -1 || strCheck2.indexOf(key) != -1 )
        {
            return false;// Not a valid key
        }
    return true;
}

The problem is if the textbox has avalue 1.55 entered and the user moves to the start position of the textbox it wont accept anything

To solve this I need to know the position of the cursor inside the textbox at the time the event was triggered.

Can anybody help??
 
Old February 14th, 2006, 05:02 PM
nzakas's Avatar
Wrox Author
 
Join Date: Dec 2004
Posts: 217
Thanks: 0
Thanked 5 Times in 5 Posts
Default

To do this, you need to get the current selection in the textbox. You should read this article to get information about text selection: http://www.webreference.com/programming/javascript/ncz/


Nicholas C. Zakas
Author, Professional JavaScript for Web Developers (ISBN 0764579088)
http://www.nczonline.net/
 
Old February 16th, 2006, 06:30 PM
Registered User
 
Join Date: Feb 2006
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to rungss Send a message via Yahoo to rungss
Default

Hi, Nicholas
I read about you in a lot of places
Any ways the tutorial there uses an approach of TypeAhead
If this is the case there is no problem with the above code.
I need to know the position of the Cursor not the selected text.
Any ways I did get some Idea that can be implemented and The desired functionality can be achieved in a different way.
I will try that


 
Old February 18th, 2006, 09:37 AM
Friend of Wrox
 
Join Date: Oct 2004
Posts: 553
Thanks: 0
Thanked 1 Time in 1 Post
Send a message via MSN to vinod_yadav1919 Send a message via Yahoo to vinod_yadav1919
Default

Hiii Rungss!!

Please try this code, you can get Cursor Postion As well as your required Validations.

Please let me know if you any doubts
<script>

function setCaretPosition(obj) {
if (!document.all) {return}
textElement =obj
if (textElement.createTextRange)
     caretPosition = document.selection.createRange();

}

function setStatus(field,event,precision22) {
var isIE = document.all?true:false;
var isNS = document.layers?true:false;
var keyCodeValue = (isIE) ? window.event.keyCode : e.which;
if ( (keyCodeValue >47&& keyCodeValue <58)||keyCodeValue ==46)
{
var icaretPosition = (caretPosition) ? caretPosition : textElement.length;
icaretPosition.text = "|";

currentValue = textElement.value;
//return /^([01]?[0-9]|[2][0-3])(:[0-5][0-9])?$/

var str = new String; // create empty string
str= String.fromCharCode(keyCodeValue); // to store keypressedvalue


original_caretPosition=currentValue.indexOf( "|")
alert("Original Positon"+original_caretPosition)

     rightbeforedot= currentValue.split(".")[0]
     dotposition=currentValue.indexOf(".")
    
     if(dotposition>original_caretPosition||dotpositi on==-1)
        {
                original_time=rightbeforedot.replace("|",str).repl ace(".","")
                //alert("temporiginal_time"+original_time)
                if(original_time<=11&&original_time.length<=2)
                {
                 event.returnValue = true;
                }
                else
                {
                 event.returnValue = false;
                     caretPosition.text="";
                }
        }

else
{ if(currentValue.indexOf(".")>=0)
        {
        dec_right=currentValue.indexOf(".")
        original_time=currentValue.replace("|",str)
        dec_left=original_time.substring(dec_right+1,origi nal_time.length)
            if(dec_left>=60||keyCodeValue==46||dec_left.length >2)
            {event.returnValue = false;
            }
        else
            { event.returnValue =true
                caretPosition.text="";
            }
        }
}
     caretPosition.moveStart("character", -1);
     caretPosition.text="";
}
else
{
event.returnValue = false;
}
}


function setDefault(obj)
{
objvalue=obj.value

deciamlindex=objvalue.indexOf(".")

if(deciamlindex!=-1)
{
finaldecimal=objvalue.substring(deciamlindex+1,o bjvalue.length)

if(finaldecimal.length==1) //only one digit after decimal place checkfor it should not be greater than 6
     {regExp=/[7-9]/
     if(regExp.test(finaldecimal))
        {
        alert("Onchange"+objvalue)
        alert("Invalid Fractional Part1")
        obj.value=""
        }
     }
else
{
     regExp=/([0-5][0-9])/g
     if(!regExp.test(finaldecimal))
        {
        alert("Onchange"+objvalue)
        alert("Invalid Fractional Part 2")
        obj.value=""
        }
}
}

}


function setDecimalValidation (field,event,precision22)
{
var isIE = document.all?true:false;
var isNS = document.layers?true:false;
var keyCodeValue = (isIE) ? window.event.keyCode : e.which;

if ( (keyCodeValue >47&& keyCodeValue <58)||keyCodeValue ==46)
{
var icaretPosition = (caretPosition) ? caretPosition : textElement.length;
icaretPosition.text = "|";
currentValue = textElement.value;
temp_original=currentValue;
original_caretPosition=currentValue.indexOf( "|")
var arr=currentValue.split("|");
leftValuetoCaret=arr[0]
rightValuetoCaret = arr[1]

if(leftValuetoCaret.indexOf(".")>=0)
        {
         dec_right=temp_original.indexOf(".")
         dec_left=temp_original.substring(dec_right+1,tem p_original.length)
         dec_left_length=dec_left.length
                    if(keyCodeValue!=46&&dec_left_length<((precision22 )+1))
                            { event.returnValue = true;}
                     else
                         { event.returnValue = false;}
     }

        if((rightValuetoCaret.indexOf(".")>=0)&&keyCodeVal ue ==46)
                     {event.returnValue = false;}
     caretPosition.moveStart("character", -1);
     caretPosition.text="";
}
else
event.returnValue = false;

}



function setOnlyNumbers(obj)
{
var isIE = document.all?true:false;
var isNS = document.layers?true:false;
var keyCodeValue = (isIE) ? window.event.keyCode : e.which;
if (keyCodeValue < 47 || keyCodeValue > 57)
event.returnValue = false;
else
event.returnValue=true;

}



</script>

</head>

<body >
<form>
<table><tr>
<td>Decimal Check Only [0-59]<input name="txt" onkeypress="setCaretPosition(this);s etStatus(this,event)" onchange="setDefault(this )"></td>
</tr>
<tr><td>Allow only 5 Digits After Decimal Point[Even cursor is placed just After Decimal Dot]<input name="txt2" onkeypress="setCaretPosition(this); setDecimalValidation(this,event,5)" ></td>

<tr><td>No Decimal Allowed(only numbers)<input name="txt32" onkeypress="setOnlyNumbers(event)" ></td>


</tr>
</table>
</form>
<br>
</body>
</html>


Hope this will help you

Cheers :)

vinod





Similar Threads
Thread Thread Starter Forum Replies Last Post
pointing cursor from one textbox to other textbox lakshmi_annayappa ASP.NET 1.0 and 1.1 Basics 2 August 2nd, 2007 03:41 PM
How to set cursor position in a textbox? zayasv VB.NET 2 April 29th, 2006 07:09 AM
Set cursor at specific location in text box pritz VB How-To 3 August 29th, 2005 10:27 AM
Set cursor at specific location in text box pritz Beginning VB 6 1 August 15th, 2005 12:22 PM
Screen cursor location takabyte Classic ASP Basics 1 June 5th, 2005 06:18 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.