2002-05-08 11:56:58 -04:00
// Author: Kelvin Tan (kelvin@relevanz.com)
2002-12-03 06:47:26 -05:00
// Date: 03/12/2002
// JavaScript Lucene Query Validator
// Version: $Id$
2002-05-08 11:56:58 -04:00
2002-12-04 19:22:50 -05:00
// Makes wildcard queries case-insensitive if true.
var wildcardCaseInsensitive = true ;
// Mutator method for wildcardCaseInsensitive.
// @param Should wildcard queries be case-insensitive?
function setWildcardCaseInsensitive ( bool )
{
wildcardCaseInsensitive = bool ;
}
2002-05-08 11:56:58 -04:00
// validates a lucene query.
// @param Form field that contains the query
function doCheckLuceneQuery ( queryField )
{
var query = queryField . value ;
if ( query != null && query . length > 0 )
{
var matches = query . match ( /^(\*)|([^a-zA-Z0-9_]\*)/ ) ;
// check wildcards are used properly
if ( matches != null && matches . length > 0 )
{
alert ( "Invalid search query! The wildcard (*) character must be preceded by at least one alphabet or number. Please try again." )
return false ;
}
// check parentheses are used properly
2002-12-03 06:47:26 -05:00
matches = query . match ( /^([^\n()]*|(\(([a-zA-Z0-9_+\-:()\" ]|\*)+\)))*$/ ) ;
2002-05-08 11:56:58 -04:00
if ( matches == null || matches . length == 0 )
{
alert ( "Invalid search query! Parentheses must contain at least one alphabet or number. Please try again." )
return false ;
}
// check '+' and '-' are used properly
matches = query . match ( /^(([^\n+-]*|[+-]([a-zA-Z0-9_:()]|\*)+))*$/ ) ;
if ( matches == null || matches . length == 0 )
{
alert ( "Invalid search query! '+' and '-' modifiers must be followed by at least one alphabet or number. Please try again." )
return false ;
}
2002-12-03 06:47:26 -05:00
2002-05-08 11:56:58 -04:00
// check that quote marks are closed
matches = query . match ( /\"/g ) ;
if ( matches != null )
{
var number = matches . length ;
if ( ( number % 2 ) > 0 )
{
alert ( "Invalid search query! Please close all quote (\") marks." ) ;
return false ;
}
}
2002-12-03 06:47:26 -05:00
2002-05-08 11:56:58 -04:00
// check ':' is used properly
2002-12-03 06:47:26 -05:00
matches = query . match ( /^(([^\n:]*|([a-zA-Z0-9_]|\*)+[:]([a-zA-Z0-9_()"]|\*)+))*$/ ) ;
2002-05-08 11:56:58 -04:00
if ( matches == null || matches . length == 0 )
{
alert ( "Invalid search query! Field declarations (:) must be preceded by at least one alphabet or number and followed by at least one alphabet or number. Please try again." )
return false ;
}
2002-12-03 06:47:26 -05:00
if ( wildcardCaseInsensitive )
{
if ( query . indexOf ( "*" ) != - 1 )
{
queryField . value = query . toLowerCase ( ) ;
}
}
2002-05-08 11:56:58 -04:00
return true ;
}
}