NEWBY: How to reconize a numeric or alphabetic entry??

Questions, tips and tricks and techniques for scripting in Servoy

NEWBY: How to reconize a numeric or alphabetic entry??

Postby zoo » Wed Sep 01, 2010 2:17 pm

Well this is the question that i have.

I'm trying to make a global search field.
In this search field we could enter a numbers o alphanumeric chars. Numbers chars, could be a DATE or ID. So, the routine need be intelligent to recognize both numeric types of that fields (DATE / ID) and additionally, know if a char entry.

How can i do that?

Thanks for your help and sorry for my english :oops:


This is my routine right know:

Code: Select all
/**
* @properties={typeid:35,uuid:"f433f666-0c01-470c-a646-2a31f6337615",variableType:4}
*/
// var _recno = 0;

/**
* @properties={typeid:35,uuid:"724135a7-b78a-466e-8e89-57e350291b60"}
*/
var _searchField = '';

/**
* @properties={typeid:24,uuid:"c7786092-2f3c-45c2-84b9-03b69748c73c"}
*/
function ejecuta_busqueda()
{
   databaseManager.setAutoSave(false)
   foundset.loadAllRecords()
   var _nr = 0;
      if (foundset.find())
      {
         application.output(_searchField)
         if (_searchField)                     // Si el campo _searchField tiene algo hacer:
            {
               apellido = "#%" + _searchField + "%"      // asigno a la variable de "tab.pacientes.apellido", el contenido del campo de búsqueda _searchField
               foundset.newRecord(false)            // ejecuto la búsqueda con false para que me muestre todas las coincidencias
               nombre   = "#%" + _searchField + "%"      // asigno a la variable de "tab.pacientes.nombre", el contenido del campo de búsqueda _searchField   
               foundset.newRecord(false)            // ejecuto la búsqueda con false para que me muestre todas las coincidencias
//               fecha    = "#%" + _searchField + "%"      // asigno a la variable de "tab.pacientes.fecha", el contenido del campo de búsqueda _searchField
//               foundset.newRecord(false)            // ejecuto la búsqueda con false para que me muestre todas las coincidencias
            }
            else
            {
               fecha     = '#' + _searchField
               foundset.newRecord(false)               // ejecuto la búsqueda con false para que me muestre todas las coincidencias
            }
         else
         {
            apellido = null
            nombre = null
            fecha    = null
         }
         _nr = foundset.search(true,false)
//                        // true en el primer parametro = si tenias un busqueda anterior, borrarla
//                        // false en el segundo parametro = concidencias no exactas
//         application.output(_nr);      
      }
}

/**
* @properties={typeid:24,uuid:"fa0fb8eb-8e42-4e1b-b638-452fe141e72d"}
*/
function limpia_filtro()
{
   application.output('Filtro eliminado...')
   foundset.loadAllRecords();
   _searchField = null;
}
zoo
 
Posts: 14
Joined: Tue Aug 24, 2010 2:39 pm

Re: NEWBY: How to reconize a numeric or alphabetic entry??

Postby kwpsd » Wed Sep 01, 2010 7:15 pm

Use the 'typeof()' method located in the JSLib under Special Operators. It will return the object type as follows:

Returns the type of the given object, one of these get returned: number, string, boolean, object, function, undefined.


I hope this helps!
Kim W. Premuda
San Diego, CA USA
User avatar
kwpsd
 
Posts: 687
Joined: Sat Jul 28, 2007 6:59 pm
Location: San Diego, CA USA

Re: NEWBY: How to reconize a numeric or alphabetic entry??

Postby lwjwillemsen » Wed Sep 01, 2010 8:46 pm

FYI :

And what will the null value return ?? Answer : Object

Regards,
Lambert Willemsen
Vision Development BV
lwjwillemsen
 
Posts: 680
Joined: Sat Mar 14, 2009 5:39 pm
Location: The Netherlands

Re: NEWBY: How to reconize a numeric or alphabetic entry??

Postby john.allen » Thu Sep 02, 2010 1:01 am

I imagine though that you'll want to determine whether it is a date (say dd/mm/yyyy), a number containing only numbers or alphabetic characters as if it is a search field presumably you do different things depending on the type of value it is. To do that you'll probably need to run some regular expression (match or search under JS Lib/String) to check, for instance, if the date is properly formatted or there are a mix of characters when it should just be one or the other (numbers or letters).
John Allen
Stanford University
john.allen
 
Posts: 515
Joined: Wed Jul 02, 2003 10:07 pm
Location: Stanford CA USA

Re: NEWBY: How to reconize a numeric or alphabetic entry??

Postby zoo » Sat Sep 04, 2010 8:40 am

Thanks for yours answers, but i'm not clear yet :(

How to say this:

if (typeof(_searchField) = A STRING)
{
bla,bla...
}
else
{
bla, bla...
}

Explain me easy, please, like a little boy... I'm a newbie :roll:
zoo
 
Posts: 14
Joined: Tue Aug 24, 2010 2:39 pm

Re: NEWBY: How to reconize a numeric or alphabetic entry??

Postby kwpsd » Sun Sep 05, 2010 2:34 am

Like so:

Code: Select all
if ( typeof( _searchField ) == 'string' )


Check our the Servoy Wiki for more examples:

http://wiki.servoy.com/display/Serv51/Special+Operators

I hope this helps!
Kim W. Premuda
San Diego, CA USA
User avatar
kwpsd
 
Posts: 687
Joined: Sat Jul 28, 2007 6:59 pm
Location: San Diego, CA USA

Re: NEWBY: How to reconize a numeric or alphabetic entry??

Postby Joas » Mon Sep 06, 2010 10:39 am

kwpsd wrote:Like so:

Unfortunately that will not work. Because the user input is entered in a text field, the type of the input will always be "string".

I think you should use a couple of regular expressions to recognize numbers and dates and otherwise default to string, like this:
Code: Select all
if (/^\d*$/.test(_searchField)) { //number
   ...
} else if (/^\d{2}.\d{2}.\d{4}$/.test(_searchField)) { //date
   ...
} else { //string
   ...
}

If you want to know more about regular expressions, here is a small tutorial.
Joas de Haan
Yield Software Development
Need help on your project? yieldsd.com
User avatar
Joas
Site Admin
 
Posts: 842
Joined: Mon Mar 20, 2006 4:07 pm
Location: Leusden, NL

Re: NEWBY: How to reconize a numeric or alphabetic entry??

Postby zoo » Wed Sep 08, 2010 5:07 am

Joas, yes this is a one way to do what i want :)

But i'd found other way using isNaN() :D

Code: Select all
// ej. using isNaN() function

var _searchfield = 99

if (!isNaN(_searchfield))
{
do something
}
else
{
do anotherthing
}


Well, thanks for all yours answers. I appreciate your help :)
zoo
 
Posts: 14
Joined: Tue Aug 24, 2010 2:39 pm


Return to Methods

Who is online

Users browsing this forum: No registered users and 11 guests

cron