0

i have a web application. In the customer form i make a empty validation, but now i have to test if the value has a special characters to, like a: ÁÄÉëÍÏ......

this is the .java empty validation:

private void testFields(String name) {

    if (applicationField == null) {
        LOG.error("The request is invalid");
    }

    try {
        Field f = applicationField.class.getDeclaredField(name);
        f.setAccessible(true);

        Object value = f.get(applicationField);

        boolean emp = false;

        if (value instanceof String) {
            String strVal = (String) value;
            if (StringUtils.isEmpty(strVal)) {
                emp = true;
            }
        } else if (value == null) {
            emp = true;
        }

        if (emp) {
            addMissingField(name);
        }

    } catch (Exception e) {
        LOG.error("Failed to validate the field: " + name, e);
    }
}

private void addMissingField(String name){
    if (StringUtils.isEmpty(missingFields)) {
        missingFields = name;
    } else {
        missingFields += " " + name;
    }       
}

In the .jsp i make this in the submit button:

    $("#submit")
        .button()
        .click(function(event) {
            event.preventDefault();

            var submitButton = $(this); 

            submitButton.button("disable");

            $("input:text").each(function(idx,elem){
                $(elem).val($(elem).val().toUpperCase());       
            });

            var form = $("#customerForm");
            var action = form.attr('action');
            var queryString = form.serialize();

            console.log(notifier);

            var taskId = notifier.start("Processing Request");

            $.ajax({
                method: "post",
                url: action,
                data: queryString,
                statusCode: {
                    201: function() {
                        window.location.replace(".........");
                    }
                }
            }).always(function(data){

                notifier.done(taskId);
                submitButton.button("enable");

            }).done(function(data){

                if(data.missingFields){
                    messageDialog("Sorry, Missing required fields to register the application", true);
                    markMissingFields(data.missingFields);
                    console.log("This is the missing Fields: " + data.missingFields);
                } 

            }).fail(function(data){                                     
                messageDialog("There was an error processing the request", true);                   
            });

        });

So, the empty validation works perfectly. But sometime the users insert data like a: MÖDRIC, JHÖN or CÁRLOS creating problems in the difference procedure of the database.

I wanna know how can i do to include a special characters validation testing if the values are empty or has a special characters at the same time.

So i need a good way to make this in the .java or .jsp.

spikeTJ
  • 73
  • 1
  • 13

3 Answers3

1

in javascript you can use Regular Expressions:

var str = "ábcde";
var str2 = "abcd";
var patt = new RegExp("[^a-zA-Z0-9\-\\/(),_\s]+") 
var res = patt.test(str);
patt.test(str); //true
patt.test(str2); //false
ohrlando
  • 106
  • 11
0

Try

var charMap = {
                "á": "a",
                "à": "a",
                "â": "a",
                "á": "a",
                "é": "e",
                "è": "e",
                "ê": "e",
                "ë": "e",
                "ï": "i",
                "î": "i",
                "ô": "o",
                "ö": "o",
                "û": "u",
                "ù": "u"
            };

var str = "MÖDRIC";

var normalize = function normalize(item) {
  var res = $.map(charMap, function(val, key) {
    return $.inArray(key.toUpperCase(), item.split("")) !== -1 
           ? item.replace(key.toUpperCase(), val.toUpperCase()) : null
  })[0];  
  return res.length ? res : item
};

console.log(normalize(str),  normalize("JHÖN"), normalize("CÁRLOS"));

 // usage within `js` at OP

 $("input:text").each(function(idx, elem){
     $(elem).val(normalize(elem.value));       
 });

jsfiddle http://jsfiddle.net/guest271314/m6d0du7z/

See also How to make matcher ignore diacritics - JQUERY

    var charMap = {
                    "á": "a",
                    "à": "a",
                    "â": "a",
                    "á": "a",
                    "é": "e",
                    "è": "e",
                    "ê": "e",
                    "ë": "e",
                    "ï": "i",
                    "î": "i",
                    "ô": "o",
                    "ö": "o",
                    "û": "u",
                    "ù": "u"
                };

    var str = "MÖDRIC";
    
    var normalize = function normalize(item) {
      var res = $.map(charMap, function(val, key) {
        return $.inArray(key.toUpperCase(), item.split("")) !== -1 
               ? item.replace(key.toUpperCase(), val.toUpperCase()) : null
      })[0];  
      return res.length ? res : item
    };
    
    console.log(normalize(str),  normalize("JHÖN"), normalize("CÁRLOS"));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
Community
  • 1
  • 1
guest271314
  • 1
  • 15
  • 104
  • 177
  • I understand but, i dind find the way to do this in my code. – spikeTJ Feb 11 '15 at 20:41
  • it doesn't work yet, in the console: TypeError: normalize is not a function. – spikeTJ Feb 12 '15 at 15:31
  • f possible , can create "stacksnippets" http://blog.stackoverflow.com/2014/09/introducing-runnable-javascript-css-and-html-code-snippets/ , jsfiddle http://jsfiddle.net to demonstrate ? ? – guest271314 Feb 12 '15 at 15:35
  • Sorry i cant use it. But i just include in my code: var charMap, var normalize and the update post. And i got this message in the console: TypeError: normalize is not a function. $(elem).val(normalize(elem.value)); @guest271314 – spikeTJ Feb 12 '15 at 21:15
0

I managed to solve this situation by implementing this code:

$('#inputid, #inputid').bind('keypress', function (event) {
    var regex = new RegExp(/^[a-zA-Z ]+$/); //Note i left a white space between brackets.
    var key = String.fromCharCode(!event.charCode ? event.which : event.charCode);
            if (!regex.test(key)) {
               event.preventDefault();
               return true;
            }
    });

Now I prevent users to enter information such as:

CHARLÍE, PETE#R, MöDRIC 12.

But they can enter names like a:

CHARLIE DA COSTE, JHON, PETER FARGO
spikeTJ
  • 73
  • 1
  • 13